public
Fork of mojombo/chronic
Description: "Chronic is a pure Ruby natural language date parser." + improvements, corrections, speedups, and additions
Homepage: http://chronic.rubyforge.org
Clone URL: git://github.com/jf/chronic.git
Search Repo:
jf (author)
Mon Apr 14 09:53:03 -0700 2008
commit  0d25ff1f36314716305efa6398272e1e6ef1a055
tree    c5adb67abea1d3d8a5bec9ed0e058cd57116ebc6
parent  8a931393e9499795a5c53a8fb8208842643cb38b
chronic / lib / chronic / ordinal.rb
100644 40 lines (34 sloc) 0.844 kb
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
module Chronic
 
  class Ordinal < Tag #:nodoc:
    def self.scan(tokens)
      # for each token
      tokens.each_index do |i|
        if t = self.scan_for_ordinals(tokens[i]) then tokens[i].tag(t) end
        if t = self.scan_for_days(tokens[i]) then tokens[i].tag(t) end
      end
      tokens
    end
  
    def self.scan_for_ordinals(token)
      if token.word =~ /^(\d*)(st|nd|rd|th)$/
        return Ordinal.new($1.to_i)
      end
      return nil
    end
    
    def self.scan_for_days(token)
      if token.word =~ /^(\d*)(st|nd|rd|th)$/
        unless $1.to_i > 31 && $1.to_i < 1
          return OrdinalDay.new(token.word.to_i)
        end
      end
      return nil
    end
    
    def to_s
      'ordinal'
    end
  end
  
  class OrdinalDay < Ordinal #:nodoc:
    def to_s
      super << '-day-' << @type.to_s
    end
  end
 
end