Skip to content
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

Serialize and deserialize week start day #75

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ schedule.add_recurrence_rule Rule.weekly(2).day(:monday, :tuesday)

# for programatic convenience (same as above)
schedule.add_recurrence_rule Rule.weekly(2).day(1, 2)

# specifying a weekly interval with a different first weekday (defaults to Sunday)
schedule.add_recurrence_rule Rule.weekly(2, :monday)
```

### Monthly (by day of month)
Expand Down
1 change: 1 addition & 0 deletions lib/ice_cube/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def self.from_hash(original_hash)
hash = IceCube::FlexibleHash.new original_hash
return nil unless match = hash[:rule_type].match(/\:\:(.+?)Rule/)
rule = IceCube::Rule.send(match[1].downcase.to_sym, hash[:interval] || 1)
rule.interval(hash[:interval] || 1, TimeUtil.daynum_to_symbol(hash[:week_start] || 0)) if match[1] == "Weekly"
rule.until(TimeUtil.deserialize_time(hash[:until])) if hash[:until]
rule.count(hash[:count]) if hash[:count]
hash[:validations] && hash[:validations].each do |key, value|
Expand Down
7 changes: 7 additions & 0 deletions lib/ice_cube/time_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ def self.week_start(sym)
def self.normalize_weekday(daynum, week_start)
(daynum - symbol_to_day(week_start)) % 7
end

# Convert day number to day symbol
def self.daynum_to_symbol(daynum)
raise "No such day number: #{daynum}" unless (0..6).include?(daynum)
day = DAYS.invert[daynum]
day
end

# Return the count of the number of times wday appears in the month,
# and which of those time falls on
Expand Down
7 changes: 7 additions & 0 deletions lib/ice_cube/validations/weekly_interval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ module IceCube
module Validations::WeeklyInterval

def interval(interval, week_start = :sunday)
@week_start = week_start
@interval = interval
validations_for(:interval) << Validation.new(interval, week_start)
clobber_base_validations(:day)
self
end

def week_start
@week_start
end


class Validation

Expand All @@ -33,6 +39,7 @@ def build_ical(builder)

def build_hash(builder)
builder[:interval] = interval
builder[:week_start] = TimeUtil.symbol_to_day(week_start)
end

def initialize(interval, week_start)
Expand Down
12 changes: 12 additions & 0 deletions spec/examples/to_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@
rule = IceCube::Rule.from_yaml rule.to_yaml
rule.occurrence_count.should == 5
end

it 'should be able to bring a Rule to_yaml and back with an undefined week start' do
rule = IceCube::Rule.weekly(2)
rule = IceCube::Rule.from_yaml rule.to_yaml
rule.week_start.should == :sunday
end

it 'should be able to bring a Rule to_yaml and back with a week start defined' do
rule = IceCube::Rule.weekly.interval(2, :monday)
rule = IceCube::Rule.from_yaml rule.to_yaml
rule.week_start.should == :monday
end

it 'should be able to bring in a schedule with a rule from hash with symbols or strings' do
time = Time.zone.now
Expand Down