Skip to content
Draft
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- some validation for ensuring BYSETPOS rule is used in conjunction with another BY* part rule. ([ignitionapp/ice_cube #5](https://github.com/ignitionapp/ice_cube/pull/5))

### Added
- Indonesian translations. ([#505](https://github.com/seejohnrun/ice_cube/pull/505)) by [@achmiral](https://github.com/achmiral)
- Support for BYSETPOS, including tests, and fixes for minor bugs (ie. Rule.from_hash, and Rule.to_s)
Expand Down
4 changes: 2 additions & 2 deletions lib/ice_cube/flexible_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def delete(key)

def _match_key(key)
return key if __getobj__.has_key? key
if Symbol == key.class
if key.instance_of?(Symbol)
__getobj__.keys.detect { |k| return k if k == key.to_s }
elsif String == key.class
elsif key.instance_of(String)
__getobj__.keys.detect { |k| return k if k.to_s == key }
end
key
Expand Down
26 changes: 26 additions & 0 deletions lib/ice_cube/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,39 @@ def from_hash(original_hash)
apply_validation(rule, name, args)
end

unless hash[:validations][:by_set_pos].nil?
unless validate_by_set_pos_by_parts(rule, hash[:validations])
raise ArgumentError, "BYSETPOS must be used in conjuction with another BY* rule part"
end
end

unless hash[:by_set_pos].nil?
rule.send(:by_set_pos, hash[:by_set_pos])
end

rule
end

def validate_by_set_pos_by_parts(rule, validations)
by_parts = [
:month_of_year,
:week_of_year,
:day_of_year,
:day_of_month,
:day,
:day_of_week,
:hour_of_day,
:minute_of_hour,
:second_of_minute
]

if !!validations[:by_set_pos]
by_parts.any? { |part| validations[part] }
else
true
end
end

private

def apply_validation(rule, name, args)
Expand Down
10 changes: 10 additions & 0 deletions spec/examples/from_ical_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ def sorted_ical(ical)
str = "FREQ=DAILY;FAKE=23"
expect { Rule.from_ical(str) }.to raise_error(ArgumentError, "Invalid rule validation type: FAKE")
end

it "should raise ArgumentError when parsing BYSETPOS without another BY* part rule" do
str = "FREQ=MONTHLY;BYSETPOS=-1"
expect { Rule.from_ical(str) }.to raise_error(ArgumentError, "BYSETPOS must be used in conjuction with another BY* rule part")
end
end

describe "multiple rules" do
Expand Down Expand Up @@ -443,6 +448,11 @@ def sorted_ical(ical)
it_behaves_like "an invalid ical string"
end

describe "BYSETPOS without another BY* part rule" do
let(:ical_str) { "RRULE:FREQ=MONTHLY;BYSETPOS=-1" }
it_behaves_like "an invalid ical string"
end

describe "invalid rule with invalid sensitive key" do
let(:ical_str) { "RRULE:FREQ=WEKLY;WKST=SU" }
it_behaves_like "an invalid ical string"
Expand Down