texel-recurrence-rule-parser 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rrule_parser.rb +88 -9
- metadata +3 -1
data/lib/rrule_parser.rb
CHANGED
@@ -12,7 +12,16 @@ class RruleParser
|
|
12
12
|
"TH" => Runt::Thursday,
|
13
13
|
"FR" => Runt::Friday,
|
14
14
|
"SA" => Runt::Saturday,
|
15
|
-
|
15
|
+
}
|
16
|
+
|
17
|
+
DAY_NAMES = {
|
18
|
+
"SU" => 'Sunday',
|
19
|
+
"MO" => 'Monday',
|
20
|
+
"TU" => 'Tuesday',
|
21
|
+
"WE" => 'Wednesday',
|
22
|
+
"TH" => 'Thursday',
|
23
|
+
"FR" => 'Friday',
|
24
|
+
"SA" => 'Saturday',
|
16
25
|
}
|
17
26
|
|
18
27
|
ADVERB_MAP = {
|
@@ -57,6 +66,14 @@ class RruleParser
|
|
57
66
|
self.expressions.inject {|m, v| v & m}
|
58
67
|
end
|
59
68
|
|
69
|
+
def frequency
|
70
|
+
self.rules[:frequency]
|
71
|
+
end
|
72
|
+
|
73
|
+
def interval
|
74
|
+
self.rules[:interval].to_i || 1
|
75
|
+
end
|
76
|
+
|
60
77
|
# Accepts a range of dates and outputs an array of dates matching the temporal expression.
|
61
78
|
def dates(range)
|
62
79
|
dates = []
|
@@ -99,15 +116,33 @@ class RruleParser
|
|
99
116
|
# Override rules to_s
|
100
117
|
rules.instance_eval do
|
101
118
|
def to_s
|
102
|
-
|
103
|
-
|
104
|
-
|
119
|
+
# Enforce order: FREQ, INTERVAL, BYDAY
|
120
|
+
ordered = [:freq, :interval, :byday]
|
121
|
+
ordered_values = ordered.map do |key|
|
122
|
+
"#{key.to_s.upcase}=#{self[key].map.join(',')}" if self[key]
|
123
|
+
end
|
124
|
+
|
125
|
+
unordered_values = self.map do |key, value|
|
126
|
+
"#{key.to_s.upcase}=#{value.map.join(',')}" unless ordered.include?(key)
|
127
|
+
end
|
128
|
+
|
129
|
+
all_values = (ordered_values + unordered_values).compact.join(';')
|
105
130
|
end
|
106
131
|
end
|
107
132
|
|
108
133
|
rules
|
109
134
|
end
|
110
135
|
|
136
|
+
def to_s
|
137
|
+
clauses = [freq_interval_clause, byday_clause, until_clause].compact.join(' ')
|
138
|
+
|
139
|
+
if clauses.blank?
|
140
|
+
'Never'
|
141
|
+
else
|
142
|
+
"Every #{clauses}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
111
146
|
protected
|
112
147
|
|
113
148
|
def parse_rules
|
@@ -129,15 +164,15 @@ class RruleParser
|
|
129
164
|
end
|
130
165
|
|
131
166
|
def parse_frequency_and_interval
|
132
|
-
if
|
133
|
-
frequency
|
134
|
-
|
135
|
-
|
167
|
+
if frequency
|
168
|
+
unless frequency == 'DAILY'
|
169
|
+
Runt::EveryTE.new(self.event.start, interval, Runt::DPrecision.const_get(ADVERB_MAP[frequency]))
|
170
|
+
end
|
136
171
|
end
|
137
172
|
end
|
138
173
|
|
139
174
|
def parse_daily
|
140
|
-
|
175
|
+
Runt::DayIntervalTE.new(self.event.start, interval)
|
141
176
|
end
|
142
177
|
|
143
178
|
def parse_weekly
|
@@ -205,4 +240,48 @@ class RruleParser
|
|
205
240
|
Runt::DIWeek.new(RruleParser::DAYS[day_string])
|
206
241
|
end
|
207
242
|
end
|
243
|
+
|
244
|
+
def pluralize(count, word)
|
245
|
+
if count == 1
|
246
|
+
word
|
247
|
+
else
|
248
|
+
"#{count} #{word}s"
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
def freq_interval_clause
|
253
|
+
return nil unless rules[:freq] && rules[:interval]
|
254
|
+
|
255
|
+
pluralize(rules[:interval].to_i, ADVERB_MAP[rules[:freq]].downcase)
|
256
|
+
end
|
257
|
+
|
258
|
+
def byday_clause
|
259
|
+
return nil unless rules[:byday]
|
260
|
+
|
261
|
+
days = rules[:byday].map { |abbr| DAY_NAMES[abbr] }
|
262
|
+
|
263
|
+
output =
|
264
|
+
case days.length
|
265
|
+
when 0
|
266
|
+
return nil
|
267
|
+
when 1
|
268
|
+
days.first
|
269
|
+
when 2
|
270
|
+
days.join(' and ')
|
271
|
+
else
|
272
|
+
last_day = days.pop
|
273
|
+
other_days = days.join(', ')
|
274
|
+
[other_days, last_day].join(', and ')
|
275
|
+
end
|
276
|
+
|
277
|
+
"on #{output}" if output
|
278
|
+
end
|
279
|
+
|
280
|
+
def until_clause
|
281
|
+
return nil unless rules[:until]
|
282
|
+
|
283
|
+
time = Time.zone.parse rules[:until]
|
284
|
+
|
285
|
+
"until #{time.strftime("%B %d, %Y")}"
|
286
|
+
end
|
208
287
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: texel-recurrence-rule-parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leigh Caplan
|
@@ -14,6 +14,7 @@ default_executable:
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: texel-runt
|
17
|
+
type: :runtime
|
17
18
|
version_requirement:
|
18
19
|
version_requirements: !ruby/object:Gem::Requirement
|
19
20
|
requirements:
|
@@ -23,6 +24,7 @@ dependencies:
|
|
23
24
|
version:
|
24
25
|
- !ruby/object:Gem::Dependency
|
25
26
|
name: icalendar
|
27
|
+
type: :runtime
|
26
28
|
version_requirement:
|
27
29
|
version_requirements: !ruby/object:Gem::Requirement
|
28
30
|
requirements:
|