Class: Chronic::Ordinal

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

Direct Known Subclasses

OrdinalDay, OrdinalMonth, OrdinalYear

Instance Attribute Summary

Attributes inherited from Tag

#type, #width

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
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chronic/tags/ordinal.rb', line 11

def self.scan(tokens, options)
  tokens.each_index do |i|
    if tokens[i].word =~ /^(\d+)(st|nd|rd|th|\.)$/
      width = $1.length
      ordinal = $1.to_i
      tokens[i].tag(Ordinal.new(ordinal, width))
      tokens[i].tag(OrdinalDay.new(ordinal, width)) if Chronic::Date::could_be_day?(ordinal, width)
      tokens[i].tag(OrdinalMonth.new(ordinal, width)) if Chronic::Date::could_be_month?(ordinal, width)
      if Chronic::Date::could_be_year?(ordinal, width)
        year = Chronic::Date::make_year(ordinal, options[:ambiguous_year_future_bias])
        tokens[i].tag(OrdinalYear.new(year.to_i, width))
      end
    elsif tokens[i].word =~ /^second$/
      tokens[i].tag(Ordinal.new(2, 1))
    end
  end
end

Instance Method Details

#to_sObject



29
30
31
# File 'lib/chronic/tags/ordinal.rb', line 29

def to_s
  'ordinal'
end