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) ⇒ Array

Scan an Array of Tokens and apply any necessary Repeater tags to each token

Parameters:

  • tokens (Array<Token>)

    Array of tokens to scan

  • options (Hash)

    Options specified in Chronic.parse

Returns:

  • (Array)

    list of tokens



10
11
12
13
14
15
16
17
18
19
# File 'lib/chronic/repeater.rb', line 10

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

.scan_for_day_names(token) ⇒ RepeaterDayName?

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chronic/repeater.rb', line 55

def self.scan_for_day_names(token)
  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(?:rs)?|ers)(day)?$/ => :thursday,
    /^fr[iy](day)?$/ => :friday,
    /^sat(t?[ue]rday)?$/ => :saturday,
    /^su[nm](day)?$/ => :sunday
  }
end

.scan_for_day_portions(token) ⇒ RepeaterDayPortion?

Parameters:

Returns:



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chronic/repeater.rb', line 70

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

.scan_for_month_names(token) ⇒ RepeaterMonthName?

Parameters:

Returns:



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

def self.scan_for_month_names(token)
  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
  }
end

.scan_for_season_names(token) ⇒ RepeaterSeasonName?

Parameters:

Returns:



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

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

.scan_for_times(token) ⇒ RepeaterTime?

Parameters:

Returns:



84
85
86
# File 'lib/chronic/repeater.rb', line 84

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

.scan_for_units(token) ⇒ Repeater

Returns A new instance of a subclass of Repeater.

Parameters:

Returns:

  • (Repeater)

    A new instance of a subclass of Repeater



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chronic/repeater.rb', line 90

def self.scan_for_units(token)
  {
    /^years?$/ => :year,
    /^seasons?$/ => :season,
    /^months?$/ => :month,
    /^fortnights?$/ => :fortnight,
    /^weeks?$/ => :week,
    /^weekends?$/ => :weekend,
    /^(week|business)days?$/ => :weekday,
    /^days?$/ => :day,
    /^hours?$/ => :hour,
    /^minutes?$/ => :minute,
    /^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)
    end
  end
  return nil
end

Instance Method Details

#<=>(other) ⇒ Object



113
114
115
# File 'lib/chronic/repeater.rb', line 113

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

#next(pointer) ⇒ Object

returns the next occurance of this repeatable.



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

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

#this(pointer) ⇒ Object



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

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

#to_sObject



131
132
133
# File 'lib/chronic/repeater.rb', line 131

def to_s
  'repeater'
end

#widthObject

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



118
119
120
# File 'lib/chronic/repeater.rb', line 118

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