forked from nehresma/ice_cube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support BYSETPOS for MONTHLY AND YEARLY freq
- Loading branch information
1 parent
fb6c657
commit 04290ec
Showing
11 changed files
with
253 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,8 @@ | |
/spec/reports/ | ||
/tmp/ | ||
|
||
# rubymine | ||
.idea | ||
|
||
# rspec failure tracking | ||
.rspec_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module IceCube | ||
|
||
module Validations::MonthlyBySetPos | ||
|
||
def by_set_pos(*by_set_pos) | ||
return by_set_pos([by_set_pos]) if by_set_pos.is_a?(Fixnum) | ||
|
||
unless by_set_pos.nil? || by_set_pos.is_a?(Array) | ||
raise ArgumentError, "Expecting Array or nil value for count, got #{by_set_pos.inspect}" | ||
end | ||
by_set_pos.flatten! | ||
by_set_pos.each do |set_pos| | ||
unless (set_pos >= -366 && set_pos <= -1) || | ||
(set_pos <= 366 && set_pos >= 1) | ||
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, 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, schedule) | ||
start_of_month = TimeUtil.start_of_month step_time | ||
end_of_month = TimeUtil.end_of_month step_time | ||
|
||
|
||
new_schedule = IceCube::Schedule.new(TimeUtil.previous_month(step_time)) do |s| | ||
s.add_recurrence_rule IceCube::Rule.from_hash(rule.to_hash.reject{|k, v| [:by_set_pos, :count, :until].include? k}) | ||
end | ||
|
||
puts step_time | ||
occurrences = new_schedule.occurrences_between(start_of_month, end_of_month) | ||
p occurrences | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module IceCube | ||
|
||
module Validations::YearlyBySetPos | ||
|
||
def by_set_pos(*by_set_pos) | ||
return by_set_pos([by_set_pos]) if by_set_pos.is_a?(Fixnum) | ||
|
||
unless by_set_pos.nil? || by_set_pos.is_a?(Array) | ||
raise ArgumentError, "Expecting Array or nil value for count, got #{by_set_pos.inspect}" | ||
end | ||
by_set_pos.flatten! | ||
by_set_pos.each do |set_pos| | ||
unless (set_pos >= -366 && set_pos <= -1) || | ||
(set_pos <= 366 && set_pos >= 1) | ||
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, 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, schedule) | ||
start_of_year = TimeUtil.start_of_year step_time | ||
end_of_year = TimeUtil.end_of_year step_time | ||
|
||
|
||
new_schedule = IceCube::Schedule.new(TimeUtil.previous_year(step_time)) do |s| | ||
s.add_recurrence_rule IceCube::Rule.from_hash(rule.to_hash.reject{|k, v| [:by_set_pos, :count, :until].include? k}) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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 | ||
|
||
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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters