Class: Chronic::Ordinal

Inherits:
Tag
  • Object
show all
Defined in:
lib/chronic/ordinal.rb

Direct Known Subclasses

OrdinalDay

Instance Attribute Summary

Attributes inherited from Tag

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tag

#initialize, #start=

Constructor Details

This class inherits a constructor from Chronic::Tag

Class Method Details

.scan(tokens, options) ⇒ Object

Scan an Array of Token objects and apply any necessary Ordinal tags to each token.

tokens - An Array of tokens to scan. options - The Hash of options specified in Chronic::parse.

Returns an Array of tokens.



11
12
13
14
15
16
# File 'lib/chronic/ordinal.rb', line 11

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

.scan_for_days(token) ⇒ Object

token - The Token object we want to scan.

Returns a new Ordinal object.



28
29
30
31
32
33
34
# File 'lib/chronic/ordinal.rb', line 28

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

.scan_for_ordinals(token) ⇒ Object

token - The Token object we want to scan.

Returns a new Ordinal object.



21
22
23
# File 'lib/chronic/ordinal.rb', line 21

def self.scan_for_ordinals(token)
  Ordinal.new($1.to_i) if token.word =~ /^(\d*)(st|nd|rd|th)$/
end

Instance Method Details

#to_sObject



36
37
38
# File 'lib/chronic/ordinal.rb', line 36

def to_s
  'ordinal'
end