File: ordinal.rb

package info (click to toggle)
ruby-chronic 0.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 384 kB
  • sloc: ruby: 3,726; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,073 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Chronic
  class Ordinal < Tag

    # Scan an Array of {Token}s and apply any necessary Ordinal tags to
    # each token
    #
    # @param [Array<Token>] tokens Array of tokens to scan
    # @param [Hash] options Options specified in {Chronic.parse}
    # @return [Array] list of tokens
    def self.scan(tokens, options)
      tokens.each do |token|
        if t = scan_for_ordinals(token) then token.tag(t) end
        if t = scan_for_days(token) then token.tag(t) end
      end
    end

    # @param [Token] token
    # @return [Ordinal, nil]
    def self.scan_for_ordinals(token)
      Ordinal.new($1.to_i) if token.word =~ /^(\d*)(st|nd|rd|th)$/
    end

    # @param [Token] token
    # @return [OrdinalDay, nil]
    def self.scan_for_days(token)
      if token.word =~ /^(\d*)(st|nd|rd|th)$/
        unless $1.to_i > 31 || $1.to_i < 1
          OrdinalDay.new(token.word.to_i)
        end
      end
    end

    def to_s
      'ordinal'
    end
  end

  class OrdinalDay < Ordinal #:nodoc:
    def to_s
      super << '-day-' << @type.to_s
    end
  end

end