Class: Chronic::Repeater

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

Overview

:nodoc:

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



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/chronic/repeater.rb', line 2

def self.scan(tokens, options)
  # for each token
  tokens.each_index do |i|
    if t = self.scan_for_month_names(tokens[i]) then tokens[i].tag(t); next end
    if t = self.scan_for_day_names(tokens[i]) then tokens[i].tag(t); next end
    if t = self.scan_for_day_portions(tokens[i]) then tokens[i].tag(t); next end
    if t = self.scan_for_times(tokens[i], options) then tokens[i].tag(t); next end
    if t = self.scan_for_units(tokens[i]) then tokens[i].tag(t); next end
  end
  tokens
end

.scan_for_day_names(token) ⇒ Object



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

def self.scan_for_day_names(token)
  scanner = {/^m[ou]n(day)?$/ => :monday,
             /^t(ue|eu|oo|u|)s(day)?$/ => :tuesday,
             /^we(dnes|nds|nns)day$/ => :wednesday,
             /^wed$/ => :wednesday,
             /^th(urs|ers)day$/ => :thursday,
             /^thu$/ => :thursday,
             /^fr[iy](day)?$/ => :friday,
             /^sat(t?[ue]rday)?$/ => :saturday,
             /^su[nm](day)?$/ => :sunday}
  scanner.keys.each do |scanner_item|
    return Chronic::RepeaterDayName.new(scanner[scanner_item]) if scanner_item =~ token.word
  end
  return nil
end

.scan_for_day_portions(token) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/chronic/repeater.rb', line 49

def self.scan_for_day_portions(token)
  scanner = {/^ams?$/ => :am,
             /^pms?$/ => :pm,
             /^mornings?$/ => :morning,
             /^afternoons?$/ => :afternoon,
             /^evenings?$/ => :evening,
             /^(night|nite)s?$/ => :night}
  scanner.keys.each do |scanner_item|
    return Chronic::RepeaterDayPortion.new(scanner[scanner_item]) if scanner_item =~ token.word
  end
  return nil
end

.scan_for_month_names(token) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/chronic/repeater.rb', line 14

def self.scan_for_month_names(token)
  scanner = {/^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}
  scanner.keys.each do |scanner_item|
    return Chronic::RepeaterMonthName.new(scanner[scanner_item]) if scanner_item =~ token.word
  end
  return nil
end

.scan_for_times(token, options) ⇒ Object



62
63
64
65
66
67
# File 'lib/chronic/repeater.rb', line 62

def self.scan_for_times(token, options)
  if token.word =~ /^\d{1,2}(:?\d{2})?([\.:]?\d{2})?$/
    return Chronic::RepeaterTime.new(token.word, options)
  end
  return nil
end

.scan_for_units(token) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chronic/repeater.rb', line 69

def self.scan_for_units(token)
  scanner = {/^years?$/ => :year,
             /^seasons?$/ => :season,
             /^months?$/ => :month,
             /^fortnights?$/ => :fortnight,
             /^weeks?$/ => :week,
             /^weekends?$/ => :weekend,
             /^days?$/ => :day,
             /^hours?$/ => :hour,
             /^minutes?$/ => :minute,
             /^seconds?$/ => :second}
  scanner.keys.each do |scanner_item|
    if scanner_item =~ token.word
      klass_name = 'Chronic::Repeater' + scanner[scanner_item].to_s.capitalize
      klass = eval(klass_name)
      return klass.new(scanner[scanner_item]) 
    end
  end
  return nil
end

Instance Method Details

#<=>(other) ⇒ Object



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

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

#next(pointer) ⇒ Object

returns the next occurance of this repeatable.



100
101
102
103
104
# File 'lib/chronic/repeater.rb', line 100

def next(pointer)
  !@now.nil? || raise("Start point must be set before calling #next")
  [:future, :none, :past].include?(pointer) || raise("First argument 'pointer' must be one of :past or :future")
  #raise("Repeatable#next must be overridden in subclasses")
end

#this(pointer) ⇒ Object



106
107
108
109
# File 'lib/chronic/repeater.rb', line 106

def this(pointer)
  !@now.nil? || raise("Start point must be set before calling #this")
  [:future, :past, :none].include?(pointer) || raise("First argument 'pointer' must be one of :past, :future, :none")
end

#to_sObject



111
112
113
# File 'lib/chronic/repeater.rb', line 111

def to_s
  'repeater'
end

#widthObject

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



95
96
97
# File 'lib/chronic/repeater.rb', line 95

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