Skip to content

Commit

Permalink
update PR from feedback
Browse files Browse the repository at this point in the history
rebased against master -- its been 4 years
  • Loading branch information
nehresma committed Dec 12, 2022
1 parent 115d5a8 commit b3042c6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 80 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@
/spec/reports/
/tmp/

# rubymine
.idea

# rspec failure tracking
.rspec_status
30 changes: 0 additions & 30 deletions lib/ice_cube/time_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,6 @@ def self.which_occurrence_in_month(time, wday)
[nth_occurrence_of_weekday, this_weekday_in_month_count]
end

# Use Activesupport CoreExt functions to manipulate time
def self.start_of_month time
time.beginning_of_month
end

# Use Activesupport CoreExt functions to manipulate time
def self.end_of_month time
time.end_of_month
end

# Use Activesupport CoreExt functions to manipulate time
def self.start_of_year time
time.beginning_of_year
end

# Use Activesupport CoreExt functions to manipulate time
def self.end_of_year time
time.end_of_year
end

# Use Activesupport CoreExt functions to manipulate time
def self.previous_month time
time - 1.month
end

# Use Activesupport CoreExt functions to manipulate time
def self.previous_year time
time - 1.year
end

# Get the days in the month for +time
def self.days_in_month(time)
date = Date.new(time.year, time.month, 1)
Expand Down
22 changes: 6 additions & 16 deletions lib/ice_cube/validations/monthly_by_set_pos.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
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?(Integer)

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)
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, by_set_pos && [Validation.new(by_set_pos, self)])
replace_validations_for(:by_set_pos, [Validation.new(by_set_pos, self)])
self
end

Expand All @@ -26,7 +19,6 @@ class Validation
attr_reader :rule, :by_set_pos

def initialize(by_set_pos, rule)

@by_set_pos = by_set_pos
@rule = rule
end
Expand All @@ -40,12 +32,11 @@ def dst_adjust?
end

def validate(step_time, schedule)
start_of_month = TimeUtil.start_of_month step_time
end_of_month = TimeUtil.end_of_month step_time

start_of_month = step_time.beginning_of_month
end_of_month = step_time.end_of_month

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})
new_schedule = IceCube::Schedule.new(step_time - 1.month) do |s|
s.add_recurrence_rule(IceCube::Rule.from_hash(rule.to_hash.except(:by_set_pos, :count, :util)))
end

occurrences = new_schedule.occurrences_between(start_of_month, end_of_month)
Expand All @@ -64,7 +55,6 @@ def validate(step_time, schedule)
end
end


def build_s(builder)
builder.piece(:by_set_pos) << by_set_pos
end
Expand Down
25 changes: 6 additions & 19 deletions lib/ice_cube/validations/yearly_by_set_pos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ 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?(Integer)

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)
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, by_set_pos && [Validation.new(by_set_pos, self)])
replace_validations_for(:by_set_pos, [Validation.new(by_set_pos, self)])
self
end

Expand All @@ -40,12 +34,11 @@ def dst_adjust?
end

def validate(step_time, schedule)
start_of_year = TimeUtil.start_of_year step_time
end_of_year = TimeUtil.end_of_year step_time

start_of_year = step_time.beginning_of_year
end_of_year = step_time.end_of_year

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})
new_schedule = IceCube::Schedule.new(step_time - 1.year) do |s|
s.add_recurrence_rule(IceCube::Rule.from_hash(rule.to_hash.except(:by_set_pos, :count, :util)))
end

occurrences = new_schedule.occurrences_between(start_of_year, end_of_year)
Expand All @@ -63,12 +56,8 @@ def validate(step_time, schedule)
1
end
end



end


def build_s(builder)
builder.piece(:by_set_pos) << by_set_pos
end
Expand All @@ -83,7 +72,5 @@ def build_ical(builder)

nil
end

end

end
24 changes: 12 additions & 12 deletions spec/examples/by_set_pos_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +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)
])
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)
])
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
14 changes: 14 additions & 0 deletions spec/examples/from_ical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ module IceCube
expect(rule).to eq(IceCube::Rule.monthly.day(:monday, :wednesday).by_set_pos([-1, 1]))
end

it 'should raise when by_set_pos is out of range (BYSETPOS)' do
expect {
IceCube::Rule.from_ical("FREQ=MONTHLY;BYDAY=MO,WE;BYSETPOS=-367")
}.to raise_error(/Expecting number in \[-366, -1\] or \[1, 366\]/)

expect {
IceCube::Rule.from_ical("FREQ=MONTHLY;BYDAY=MO,WE;BYSETPOS=367")
}.to raise_error(/Expecting number in \[-366, -1\] or \[1, 366\]/)

expect {
IceCube::Rule.from_ical("FREQ=MONTHLY;BYDAY=MO,WE;BYSETPOS=0")
}.to raise_error(/Expecting number in \[-366, -1\] or \[1, 366\]/)
end

it 'should return no occurrences after daily interval with count is over' do
schedule = IceCube::Schedule.new(Time.now)
schedule.add_recurrence_rule(IceCube::Rule.from_ical("FREQ=DAILY;COUNT=5"))
Expand Down

0 comments on commit b3042c6

Please sign in to comment.