File: ordinal.rb

package info (click to toggle)
libchronic-ruby 0.2.3-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 268 kB
  • ctags: 329
  • sloc: ruby: 2,686; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 828 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
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
          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