Skip to content

Commit 24f0c4d

Browse files
committed
prefer parsing ical from Rule and Schedule
1 parent 24ce2aa commit 24f0c4d

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

lib/ice_cube/rule.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def to_ical
2727
raise MethodNotImplemented, "Expected to be overrridden by subclasses"
2828
end
2929

30+
# Convert from ical string and create a rule
31+
def self.from_ical(ical)
32+
IceCube::IcalParser.rule_from_ical(ical)
33+
end
34+
3035
# Yaml implementation
3136
def to_yaml(*args)
3237
YAML::dump(to_hash, *args)

lib/ice_cube/schedule.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,11 @@ def to_ical(force_utc = false)
332332
pieces.join("\n")
333333
end
334334

335+
# Load the schedule from ical
336+
def self.from_ical(ical, options = {})
337+
IcalParser.schedule_from_ical(ical, options)
338+
end
339+
335340
# Convert the schedule to yaml
336341
def to_yaml(*args)
337342
YAML::dump(to_hash, *args)

spec/examples/from_ical_spec.rb

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,89 @@ module IceCube
66
describe Rule, 'from_ical' do
77

88
it 'should return a IceCube DailyRule class for a basic daily rule' do
9-
rule = IceCube::IcalParser.rule_from_ical "FREQ=DAILY"
9+
rule = IceCube::Rule.from_ical "FREQ=DAILY"
1010
rule.class.should == IceCube::DailyRule
1111
end
1212

1313
it 'should return a IceCube WeeklyRule class for a basic monthly rule' do
14-
rule = IceCube::IcalParser.rule_from_ical "FREQ=WEEKLY"
14+
rule = IceCube::Rule.from_ical "FREQ=WEEKLY"
1515
rule.class.should == IceCube::WeeklyRule
1616
end
1717

1818
it 'should return a IceCube MonthlyRule class for a basic monthly rule' do
19-
rule = IceCube::IcalParser.rule_from_ical "FREQ=MONTHLY"
19+
rule = IceCube::Rule.from_ical "FREQ=MONTHLY"
2020
rule.class.should == IceCube::MonthlyRule
2121
end
2222

2323
it 'should return a IceCube YearlyRule class for a basic yearly rule' do
24-
rule = IceCube::IcalParser.rule_from_ical "FREQ=YEARLY"
24+
rule = IceCube::Rule.from_ical "FREQ=YEARLY"
2525
rule.class.should == IceCube::YearlyRule
2626
end
2727

2828
it 'should be able to parse a .day rule' do
29-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYDAY=MO,TU")
29+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYDAY=MO,TU")
3030
rule.should == IceCube::Rule.daily.day(:monday, :tuesday)
3131
end
3232

3333
it 'should be able to parse a .day_of_week rule' do
34-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYDAY=-1TU,-2TU")
34+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYDAY=-1TU,-2TU")
3535
rule.should == IceCube::Rule.daily.day_of_week(:tuesday => [-1, -2])
3636
end
3737

3838
it 'should be able to parse both .day and .day_of_week rules' do
39-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYDAY=MO,-1TU,-2TU")
39+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYDAY=MO,-1TU,-2TU")
4040
rule.should == IceCube::Rule.daily.day_of_week(:tuesday => [-1, -2]).day(:monday)
4141
end
4242

4343
it 'should be able to parse a .day_of_month rule' do
44-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYMONTHDAY=23")
44+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYMONTHDAY=23")
4545
rule.should == IceCube::Rule.daily.day_of_month(23)
4646
end
4747

4848
it 'should be able to parse a .day_of_year rule' do
49-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYYEARDAY=100,200")
49+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYYEARDAY=100,200")
5050
rule.should == IceCube::Rule.daily.day_of_year(100,200)
5151
end
5252

5353
it 'should be able to serialize a .month_of_year rule' do
54-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYMONTH=1,4")
54+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYMONTH=1,4")
5555
rule.should == IceCube::Rule.daily.month_of_year(:january, :april)
5656
end
5757

5858
it 'should be able to split to a combination of day_of_week and day (day_of_week has priority)' do
59-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYDAY=TU,MO,1MO,-1MO")
59+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYDAY=TU,MO,1MO,-1MO")
6060
rule.should == IceCube::Rule.daily.day(:tuesday).day_of_week(:monday => [1, -1])
6161
end
6262

6363
it 'should be able to parse of .day_of_week rule with multiple days' do
64-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;BYDAY=WE,1MO,-1MO,2TU")
64+
rule = IceCube::Rule.from_ical("FREQ=DAILY;BYDAY=WE,1MO,-1MO,2TU")
6565
rule.should == IceCube::Rule.daily.day_of_week(:monday => [1, -1], :tuesday => [2]).day(:wednesday)
6666
end
6767

6868
it 'should be able to parse a rule with an until date' do
6969
t = Time.now.utc
70-
rule = IceCube::IcalParser.rule_from_ical("FREQ=WEEKLY;UNTIL=#{t.strftime("%Y%m%dT%H%M%SZ")}")
70+
rule = IceCube::Rule.from_ical("FREQ=WEEKLY;UNTIL=#{t.strftime("%Y%m%dT%H%M%SZ")}")
7171
rule.to_s.should == IceCube::Rule.weekly.until(t).to_s
7272
end
7373

7474
it 'should be able to parse a rule with a count date' do
75-
rule = IceCube::IcalParser.rule_from_ical("FREQ=WEEKLY;COUNT=5")
75+
rule = IceCube::Rule.from_ical("FREQ=WEEKLY;COUNT=5")
7676
rule.should == IceCube::Rule.weekly.count(5)
7777
end
7878

7979
it 'should be able to parse a rule with an interval' do
80-
rule = IceCube::IcalParser.rule_from_ical("FREQ=DAILY;INTERVAL=2")
80+
rule = IceCube::Rule.from_ical("FREQ=DAILY;INTERVAL=2")
8181
rule.should == IceCube::Rule.daily.interval(2)
8282
end
8383

8484
it 'should be able to parse week start (WKST)' do
85-
rule = IceCube::IcalParser.rule_from_ical("FREQ=WEEKLY;INTERVAL=2;WKST=MO")
85+
rule = IceCube::Rule.from_ical("FREQ=WEEKLY;INTERVAL=2;WKST=MO")
8686
rule.should == IceCube::Rule.weekly(2, :monday)
8787
end
8888

8989
it 'should return no occurrences after daily interval with count is over' do
9090
schedule = IceCube::Schedule.new(Time.now)
91-
schedule.add_recurrence_rule(IceCube::IcalParser.rule_from_ical("FREQ=DAILY;COUNT=5"))
91+
schedule.add_recurrence_rule(IceCube::Rule.from_ical("FREQ=DAILY;COUNT=5"))
9292
schedule.occurrences_between(Time.now + 7.days, Time.now + 14.days).count.should == 0
9393
end
9494

@@ -123,7 +123,7 @@ def sorted_ical(ical)
123123

124124
describe "instantiation" do
125125
it "loads an ICAL string" do
126-
expect(IceCube::IcalParser.schedule_from_ical(ical_string)).to be_a(IceCube::Schedule)
126+
expect(IceCube::Schedule.from_ical(ical_string)).to be_a(IceCube::Schedule)
127127
end
128128
end
129129

@@ -135,7 +135,7 @@ def sorted_ical(ical)
135135
schedule.add_recurrence_rule(IceCube::Rule.daily)
136136

137137
ical = schedule.to_ical
138-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
138+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
139139
end
140140

141141
it 'handles counts' do
@@ -145,7 +145,7 @@ def sorted_ical(ical)
145145
schedule.add_recurrence_rule(IceCube::Rule.daily.count(4))
146146

147147
ical = schedule.to_ical
148-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
148+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
149149
end
150150

151151
it 'handles intervals' do
@@ -155,7 +155,7 @@ def sorted_ical(ical)
155155
schedule.add_recurrence_rule(IceCube::Rule.daily(4))
156156

157157
ical = schedule.to_ical
158-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
158+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
159159
end
160160

161161
it 'handles intervals and counts' do
@@ -165,7 +165,7 @@ def sorted_ical(ical)
165165
schedule.add_recurrence_rule(IceCube::Rule.daily(4).count(10))
166166

167167
ical = schedule.to_ical
168-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
168+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
169169
end
170170

171171
it 'handles until dates' do
@@ -175,7 +175,7 @@ def sorted_ical(ical)
175175
schedule.add_recurrence_rule(IceCube::Rule.daily.until(start_time + 15.days))
176176

177177
ical = schedule.to_ical
178-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
178+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
179179
end
180180

181181
end
@@ -188,7 +188,7 @@ def sorted_ical(ical)
188188
schedule.add_recurrence_rule(IceCube::Rule.weekly)
189189

190190
ical = schedule.to_ical
191-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
191+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
192192
end
193193

194194
it 'handles weekdays' do
@@ -198,7 +198,7 @@ def sorted_ical(ical)
198198
schedule.add_recurrence_rule(IceCube::Rule.weekly.day(:monday, :thursday))
199199

200200
ical = schedule.to_ical
201-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
201+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
202202
end
203203

204204
it 'handles intervals' do
@@ -208,7 +208,7 @@ def sorted_ical(ical)
208208
schedule.add_recurrence_rule(IceCube::Rule.weekly(2))
209209

210210
ical = schedule.to_ical
211-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
211+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
212212
end
213213

214214
it 'handles intervals and counts' do
@@ -218,7 +218,7 @@ def sorted_ical(ical)
218218
schedule.add_recurrence_rule(IceCube::Rule.weekly(2).count(4))
219219

220220
ical = schedule.to_ical
221-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
221+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
222222
end
223223

224224
it 'handles intervals and counts on given weekdays' do
@@ -228,7 +228,7 @@ def sorted_ical(ical)
228228
schedule.add_recurrence_rule(IceCube::Rule.weekly(2).day(:monday, :wednesday).count(4))
229229

230230
ical = schedule.to_ical
231-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
231+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
232232
end
233233
end
234234

@@ -240,7 +240,7 @@ def sorted_ical(ical)
240240
schedule.add_recurrence_rule(IceCube::Rule.monthly)
241241

242242
ical = schedule.to_ical
243-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
243+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
244244
end
245245

246246
it 'handles intervals' do
@@ -250,7 +250,7 @@ def sorted_ical(ical)
250250
schedule.add_recurrence_rule(IceCube::Rule.monthly(2))
251251

252252
ical = schedule.to_ical
253-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
253+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
254254
end
255255

256256
it 'handles intervals and counts' do
@@ -260,7 +260,7 @@ def sorted_ical(ical)
260260
schedule.add_recurrence_rule(IceCube::Rule.monthly(2).count(5))
261261

262262
ical = schedule.to_ical
263-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
263+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
264264
end
265265

266266
it 'handles intervals and counts on specific days' do
@@ -270,7 +270,7 @@ def sorted_ical(ical)
270270
schedule.add_recurrence_rule(IceCube::Rule.monthly(2).day_of_month(1, 15).count(5))
271271

272272
ical = schedule.to_ical
273-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
273+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
274274
end
275275
end
276276

@@ -282,7 +282,7 @@ def sorted_ical(ical)
282282
schedule.add_recurrence_rule(IceCube::Rule.yearly)
283283

284284
ical = schedule.to_ical
285-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
285+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
286286
end
287287

288288
it 'handles intervals' do
@@ -292,7 +292,7 @@ def sorted_ical(ical)
292292
schedule.add_recurrence_rule(IceCube::Rule.yearly(2))
293293

294294
ical = schedule.to_ical
295-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
295+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
296296
end
297297

298298
it 'handles a specific day' do
@@ -302,7 +302,7 @@ def sorted_ical(ical)
302302
schedule.add_recurrence_rule(IceCube::Rule.yearly.day_of_year(15))
303303

304304
ical = schedule.to_ical
305-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
305+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
306306
end
307307

308308
it 'handles specific days' do
@@ -312,7 +312,7 @@ def sorted_ical(ical)
312312
schedule.add_recurrence_rule(IceCube::Rule.yearly.day_of_year(1, 15, -1))
313313

314314
ical = schedule.to_ical
315-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
315+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
316316
end
317317

318318
it 'handles counts' do
@@ -322,7 +322,7 @@ def sorted_ical(ical)
322322
schedule.add_recurrence_rule(IceCube::Rule.yearly.count(5))
323323

324324
ical = schedule.to_ical
325-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
325+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
326326
end
327327

328328
it 'handles specific months' do
@@ -332,7 +332,7 @@ def sorted_ical(ical)
332332
schedule.add_recurrence_rule(IceCube::Rule.yearly.month_of_year(:january, :december))
333333

334334
ical = schedule.to_ical
335-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
335+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
336336
end
337337

338338
it 'handles specific months and counts' do
@@ -342,7 +342,7 @@ def sorted_ical(ical)
342342
schedule.add_recurrence_rule(IceCube::Rule.yearly.month_of_year(:january, :december).count(15))
343343

344344
ical = schedule.to_ical
345-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
345+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
346346
end
347347
end
348348

@@ -355,11 +355,11 @@ def sorted_ical(ical)
355355
schedule.add_exception_time(Time.now + 2.days)
356356

357357
ical = schedule.to_ical
358-
sorted_ical(IceCube::IcalParser.schedule_from_ical(ical).to_ical).should eq(sorted_ical(ical))
358+
sorted_ical(IceCube::Schedule.from_ical(ical).to_ical).should eq(sorted_ical(ical))
359359
end
360360

361361
it 'handles multiple EXDATE lines' do
362-
schedule = IceCube::IcalParser.schedule_from_ical ical_string_woth_multiple_exdates
362+
schedule = IceCube::Schedule.from_ical ical_string_woth_multiple_exdates
363363
schedule.exception_times.count.should == 3
364364
end
365365
end

0 commit comments

Comments
 (0)