-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update to BYSETPOS support #449
base: master
Are you sure you want to change the base?
Changes from 7 commits
136faf0
84120b5
115d5a8
b3042c6
6dd602d
c70d7f6
0f5c71a
621bf3d
ee3171d
6a50aad
d7ea51e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require "date" | ||
require "time" | ||
require 'date' | ||
require 'time' | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing the type of quotes seems unnecessary. Double quotes are consistent with the existing style. |
||
|
||
module IceCube | ||
module TimeUtil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module IceCube | ||
|
||
module Validations::MonthlyBySetPos | ||
def by_set_pos(*by_set_pos) | ||
by_set_pos.flatten! | ||
by_set_pos.each do |set_pos| | ||
unless (-366..366).include?(set_pos) && set_pos != 0 | ||
raise ArgumentError, "Expecting number in [-366, -1] or [1, 366], got #{set_pos} (#{by_set_pos})" | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
|
||
@by_set_pos = by_set_pos | ||
replace_validations_for(:by_set_pos, [Validation.new(by_set_pos, self)]) | ||
self | ||
end | ||
|
||
class Validation | ||
|
||
attr_reader :rule, :by_set_pos | ||
|
||
def initialize(by_set_pos, rule) | ||
@by_set_pos = by_set_pos | ||
@rule = rule | ||
end | ||
|
||
def type | ||
:day | ||
end | ||
|
||
def dst_adjust? | ||
true | ||
end | ||
|
||
def validate(step_time, start_time) | ||
start_of_month = step_time.beginning_of_month | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end_of_month = step_time.end_of_month | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Needs to start on the first day of the month | ||
new_schedule = IceCube::Schedule.new(start_of_month.change(hour: step_time.hour, min: step_time.min, sec: step_time.sec)) do |s| | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
s.add_recurrence_rule(IceCube::Rule.from_hash(rule.to_hash.except(:by_set_pos, :count, :until))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto my comment about |
||
end | ||
|
||
occurrences = new_schedule.occurrences_between(start_of_month, end_of_month) | ||
index = occurrences.index(step_time) | ||
if index == nil | ||
1 | ||
else | ||
positive_set_pos = index + 1 | ||
negative_set_pos = index - occurrences.length | ||
|
||
if @by_set_pos.include?(positive_set_pos) || @by_set_pos.include?(negative_set_pos) | ||
0 | ||
else | ||
1 | ||
end | ||
end | ||
end | ||
|
||
def build_s(builder) | ||
builder.piece(:by_set_pos) << by_set_pos | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @nehresma Looks like this should be improved:
|
||
end | ||
|
||
def build_hash(builder) | ||
builder[:by_set_pos] = by_set_pos | ||
end | ||
|
||
def build_ical(builder) | ||
builder['BYSETPOS'] << by_set_pos | ||
end | ||
|
||
nil | ||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
module IceCube | ||
|
||
module Validations::YearlyBySetPos | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def by_set_pos(*by_set_pos) | ||
by_set_pos.flatten! | ||
by_set_pos.each do |set_pos| | ||
unless (-366..366).include?(set_pos) && set_pos != 0 | ||
raise ArgumentError, "Expecting number in [-366, -1] or [1, 366], got #{set_pos} (#{by_set_pos})" | ||
end | ||
end | ||
|
||
@by_set_pos = by_set_pos | ||
replace_validations_for(:by_set_pos, [Validation.new(by_set_pos, self)]) | ||
self | ||
end | ||
|
||
class Validation | ||
|
||
attr_reader :rule, :by_set_pos | ||
|
||
def initialize(by_set_pos, rule) | ||
|
||
@by_set_pos = by_set_pos | ||
@rule = rule | ||
end | ||
|
||
def type | ||
:day | ||
end | ||
|
||
def dst_adjust? | ||
true | ||
end | ||
|
||
def validate(step_time, start_time) | ||
start_of_year = step_time.beginning_of_year | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end_of_year = step_time.end_of_year | ||
nehresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Needs to start on the first day of the year | ||
new_schedule = IceCube::Schedule.new(start_of_year.change(hour: step_time.hour, min: step_time.min, sec: step_time.sec)) do |s| | ||
s.add_recurrence_rule(IceCube::Rule.from_hash(rule.to_hash.except(:by_set_pos, :count, :until))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto my comment about |
||
end | ||
|
||
occurrences = new_schedule.occurrences_between(start_of_year, end_of_year) | ||
|
||
index = occurrences.index(step_time) | ||
if index == nil | ||
1 | ||
else | ||
positive_set_pos = index + 1 | ||
negative_set_pos = index - occurrences.length | ||
|
||
if @by_set_pos.include?(positive_set_pos) || @by_set_pos.include?(negative_set_pos) | ||
0 | ||
else | ||
1 | ||
end | ||
end | ||
end | ||
|
||
def build_s(builder) | ||
builder.piece(:by_set_pos) << by_set_pos | ||
end | ||
|
||
def build_hash(builder) | ||
builder[:by_set_pos] = by_set_pos | ||
end | ||
|
||
def build_ical(builder) | ||
builder['BYSETPOS'] << by_set_pos | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
module IceCube | ||
describe MonthlyRule, 'BYSETPOS' do | ||
it 'should behave correctly' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=MONTHLY;COUNT=4;BYDAY=WE;BYSETPOS=4" | ||
schedule.start_time = Time.new(2015, 5, 28, 12, 0, 0) | ||
expect(schedule.occurrences_between(Time.new(2015, 01, 01), Time.new(2017, 01, 01))). | ||
to eq([ | ||
Time.new(2015,6,24,12,0,0), | ||
Time.new(2015,7,22,12,0,0), | ||
Time.new(2015,8,26,12,0,0), | ||
Time.new(2015,9,23,12,0,0) | ||
]) | ||
end | ||
|
||
it 'should work with intervals' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=MONTHLY;COUNT=4;BYDAY=WE;BYSETPOS=4;INTERVAL=2" | ||
schedule.start_time = Time.new(2015, 5, 28, 12, 0, 0) | ||
expect(schedule.occurrences_between(Time.new(2015, 01, 01), Time.new(2017, 01, 01))). | ||
to eq([ | ||
Time.new(2015,7,22,12,0,0), | ||
Time.new(2015,9,23,12,0,0), | ||
Time.new(2015,11,25,12,0,0), | ||
Time.new(2016,1,27,12,0,0), | ||
]) | ||
end | ||
end | ||
|
||
describe YearlyRule, 'BYSETPOS' do | ||
it 'should behave correctly' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=YEARLY;BYMONTH=7;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYSETPOS=-1" | ||
schedule.start_time = Time.new(1966,7,5) | ||
expect(schedule.occurrences_between(Time.new(2015, 01, 01), Time.new(2017, 01, 01))). | ||
to eq([ | ||
Time.new(2015, 7, 31), | ||
Time.new(2016, 7, 31) | ||
]) | ||
end | ||
|
||
it 'should work with intervals' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=YEARLY;BYMONTH=7;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYSETPOS=-1;INTERVAL=2" | ||
schedule.start_time = Time.new(1966,7,5) | ||
expect(schedule.occurrences_between(Time.new(2015, 01, 01), Time.new(2023, 01, 01))). | ||
to eq([ | ||
Time.new(2016, 7, 31), | ||
Time.new(2018, 7, 31), | ||
Time.new(2020, 7, 31), | ||
Time.new(2022, 7, 31), | ||
]) | ||
end | ||
|
||
it 'should work with counts' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=YEARLY;BYMONTH=7;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYSETPOS=-1;COUNT=3" | ||
schedule.start_time = Time.new(2016,1,1) | ||
expect(schedule.occurrences_between(Time.new(2016, 01, 01), Time.new(2050, 01, 01))). | ||
to eq([ | ||
Time.new(2016, 7, 31), | ||
Time.new(2017, 7, 31), | ||
Time.new(2018, 7, 31), | ||
]) | ||
end | ||
|
||
it 'should work with counts and intervals' do | ||
schedule = IceCube::Schedule.from_ical "RRULE:FREQ=YEARLY;BYMONTH=7;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYSETPOS=-1;COUNT=3;INTERVAL=2" | ||
schedule.start_time = Time.new(2016,1,1) | ||
expect(schedule.occurrences_between(Time.new(2016, 01, 01), Time.new(2050, 01, 01))). | ||
to eq([ | ||
Time.new(2016, 7, 31), | ||
Time.new(2018, 7, 31), | ||
Time.new(2020, 7, 31), | ||
]) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ module IceCube | |
end | ||
|
||
it "should be able to make a round-trip to YAML with .day_of_year" do | ||
schedule1 = Schedule.new(Time.now) | ||
schedule1 = Schedule.new(Time.zone.now) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
schedule1.add_recurrence_rule Rule.yearly.day_of_year(100, 200) | ||
|
||
yaml_string = schedule1.to_yaml | ||
|
@@ -112,7 +112,7 @@ module IceCube | |
end | ||
|
||
it "should be able to make a round-trip to YAML with .month_of_year" do | ||
schedule = Schedule.new(Time.now) | ||
schedule = Schedule.new(Time.zone.now) | ||
schedule.add_recurrence_rule Rule.yearly.month_of_year(:april, :may) | ||
|
||
yaml_string = schedule.to_yaml | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps use the style
validations[:by_set_pos]
to match everything else in this case statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, existing code uses double quotes instead of single quotes for string in
split
andcollect
instead ofmap
. Picky but just for consistency.