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
13
14
# 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_season_names(tokens[i]) then tokens[i].tag(t); next end
		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
		if t = self.scan_for_decades(tokens[i]) then tokens[i].tag(t); next end
	end
	tokens
end

.scan_for_day_names(token) ⇒ Object



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

def self.scan_for_day_names(token)
	scanner = {/^m[ou]n(day)?$/i => :monday,
		/^t(ue|eu|oo|u|)s(day)?$/i => :tuesday,
		/^tue$/i => :tuesday,
		/^we(dnes|nds|nns)day$/i => :wednesday,
		/^wed$/i => :wednesday,
		/^th(urs|ers)day$/i => :thursday,
		/^thu$/i => :thursday,
		/^fr[iy](day)?$/i => :friday,
		/^sat(t?[ue]rday)?$/i => :saturday,
		/^su[nm](day)?$/i => :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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chronic/repeater.rb', line 64

def self.scan_for_day_portions(token)
	puts "scan_for_day_portions -- #{token}" if Chronic.debug
	scanner = {/^ams?$/i => :am,
		/^pms?$/i => :pm,
		/^mornings?$/i => :morning,
		/^afternoons?$/i => :afternoon,
		/^evenings?$/i => :evening,
		/^(night|nite)s?$/i => :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_decades(token) ⇒ Object



85
86
87
88
89
90
# File 'lib/chronic/repeater.rb', line 85

def self.scan_for_decades(token)
	if token.word =~ Chronic::RepeaterDecade::DECADE_PATTERN
		return Chronic::RepeaterDecade.new(token.word)
	end
	return nil
end

.scan_for_month_names(token) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chronic/repeater.rb', line 28

def self.scan_for_month_names(token)
	scanner = {/^jan\.?(uary)?$/i => :january,
		/^feb\.?(ruary)?$/i => :february,
		/^mar\.?(ch)?$/i => :march,
		/^apr\.?(il)?$/i => :april,
		/^may$/i => :may,
		/^jun\.?e?$/i => :june,
		/^jul\.?y?$/i => :july,
		/^aug\.?(ust)?$/i => :august,
		/^sep\.?(t\.?|tember)?$/i => :september,
		/^oct\.?(ober)?$/i => :october,
		/^nov\.?(ember)?$/i => :november,
		/^dec\.?(ember)?$/i => :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_season_names(token) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chronic/repeater.rb', line 16

def self.scan_for_season_names(token)
	scanner = {/^springs?$/i => :spring,
		/^summers?$/i => :summer,
		/^(autumn)|(fall)s?$/i => :autumn,
		/^winters?$/i => :winter}
	scanner.keys.each do |scanner_item|
		return Chronic::RepeaterSeasonName.new(scanner[scanner_item]) if scanner_item =~ token.word
	end

	return nil
end

.scan_for_times(token, options) ⇒ Object



78
79
80
81
82
83
# File 'lib/chronic/repeater.rb', line 78

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



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

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



115
116
117
# File 'lib/chronic/repeater.rb', line 115

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

#next(pointer) ⇒ Object

returns the next occurance of this repeatable.



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

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



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

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



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

def to_s
	'repeater'
end

#widthObject

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



120
121
122
# File 'lib/chronic/repeater.rb', line 120

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