Class: Chronic::Repeater

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

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 Repeater 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
# File 'lib/chronic/repeater.rb', line 11

def self.scan(tokens, options)
  tokens.each do |token|
    if t = scan_for_season_names(token, options) then token.tag(t); next end
    if t = scan_for_month_names(token, options) then token.tag(t); next end
    if t = scan_for_day_names(token, options) then token.tag(t); next end
    if t = scan_for_day_portions(token, options) then token.tag(t); next end
    if t = scan_for_times(token, options) then token.tag(t); next end
    if t = scan_for_units(token, options) then token.tag(t); next end
  end
end

.scan_for_day_names(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chronic/repeater.rb', line 59

def self.scan_for_day_names(token, options = {})
  scan_for token, RepeaterDayName,
  {
    /^m[ou]n(day)?$/ => :monday,
    /^t(ue|eu|oo|u|)s?(day)?$/ => :tuesday,
    /^we(d|dnes|nds|nns)(day)?$/ => :wednesday,
    /^th(u|ur|urs|ers)(day)?$/ => :thursday,
    /^fr[iy](day)?$/ => :friday,
    /^sat(t?[ue]rday)?$/ => :saturday,
    /^su[nm](day)?$/ => :sunday
  }, options
end

.scan_for_day_portions(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/chronic/repeater.rb', line 75

def self.scan_for_day_portions(token, options = {})
  scan_for token, RepeaterDayPortion,
  {
    /^ams?$/ => :am,
    /^pms?$/ => :pm,
    /^mornings?$/ => :morning,
    /^afternoons?$/ => :afternoon,
    /^evenings?$/ => :evening,
    /^(night|nite)s?$/ => :night
  }, options
end

.scan_for_month_names(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/chronic/repeater.rb', line 38

def self.scan_for_month_names(token, options = {})
  scan_for token, RepeaterMonthName,
  {
    /^jan[:\.]?(uary)?$/ => :january,
    /^feb[:\.]?(ruary)?$/ => :february,
    /^mar[:\.]?(ch)?$/ => :march,
    /^apr[:\.]?(il)?$/ => :april,
    /^may$/ => :may,
    /^jun[:\.]?e?$/ => :june,
    /^jul[:\.]?y?$/ => :july,
    /^aug[:\.]?(ust)?$/ => :august,
    /^sep[:\.]?(t[:\.]?|tember)?$/ => :september,
    /^oct[:\.]?(ober)?$/ => :october,
    /^nov[:\.]?(ember)?$/ => :november,
    /^dec[:\.]?(ember)?$/ => :december
  }, options
end

.scan_for_season_names(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



25
26
27
28
29
30
31
32
33
# File 'lib/chronic/repeater.rb', line 25

def self.scan_for_season_names(token, options = {})
  scan_for token, RepeaterSeasonName,
  {
    /^springs?$/ => :spring,
    /^summers?$/ => :summer,
    /^(autumn)|(fall)s?$/ => :autumn,
    /^winters?$/ => :winter
  }, options
end

.scan_for_times(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



90
91
92
# File 'lib/chronic/repeater.rb', line 90

def self.scan_for_times(token, options = {})
  scan_for token, RepeaterTime, /^\d{1,2}(:?\d{1,2})?([\.:]?\d{1,2}([\.:]\d{1,6})?)?$/, options
end

.scan_for_units(token, options = {}) ⇒ Object

token - The Token object we want to scan.

Returns a new Repeater object.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/chronic/repeater.rb', line 97

def self.scan_for_units(token, options = {})
  {
    /^years?$/ => :year,
    /^seasons?$/ => :season,
    /^months?$/ => :month,
    /^fortnights?$/ => :fortnight,
    /^weeks?$/ => :week,
    /^weekends?$/ => :weekend,
    /^(week|business)days?$/ => :weekday,
    /^days?$/ => :day,
   /^hrs?$/ => :hour,
    /^hours?$/ => :hour,
   /^mins?$/ => :minute,
    /^minutes?$/ => :minute,
   /^secs?$/ => :second,
    /^seconds?$/ => :second
  }.each do |item, symbol|
    if item =~ token.word
      klass_name = 'Repeater' + symbol.to_s.capitalize
      klass = Chronic.const_get(klass_name)
      return klass.new(symbol, options)
    end
  end
  return nil
end

Instance Method Details

#<=>(other) ⇒ Object



123
124
125
# File 'lib/chronic/repeater.rb', line 123

def <=>(other)
  width <=> other.width
end

#next(pointer) ⇒ Object

returns the next occurance of this repeatable.



133
134
135
# File 'lib/chronic/repeater.rb', line 133

def next(pointer)
  raise("Start point must be set before calling #next") unless @now
end

#this(pointer) ⇒ Object



137
138
139
# File 'lib/chronic/repeater.rb', line 137

def this(pointer)
  raise("Start point must be set before calling #this") unless @now
end

#to_sObject



141
142
143
# File 'lib/chronic/repeater.rb', line 141

def to_s
  'repeater'
end

#widthObject

returns the width (in seconds or months) of this repeatable.



128
129
130
# File 'lib/chronic/repeater.rb', line 128

def width
  raise("Repeater#width must be overridden in subclasses")
end