Skip to content

Commit

Permalink
adds occurrences method to collection class
Browse files Browse the repository at this point in the history
  • Loading branch information
XORwell committed Dec 11, 2013
1 parent 3c43993 commit 461d4a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/ice_t/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ module WeekdayMethods
def self.included(klass)
klass.instance_eval do
IceT::TimeHelper::PLURALIZED_DAYNAMES.each do |wday|
define_method(wday) { |start_date, end_date|
@rules.collect{ |rule|
rule.occurrences(start_date, end_date)
}.map { |array|
array.select(&IceT::TimeHelper.question_mark_dayname(wday))
}.flatten
.uniq
define_method(wday) { |start_time, end_time|
occurrences(start_time, end_time)
.select(&IceT::TimeHelper.question_mark_dayname(wday))
}
end
end
Expand Down Expand Up @@ -44,5 +40,12 @@ def add_rule(rule)
@rules << rule unless @rules.include?(rule)
end

def occurrences(start_time, end_time)
@rules.collect{ |rule|
rule.occurrences(start_time, end_time)
}.flatten
.uniq
end

end
end
25 changes: 25 additions & 0 deletions spec/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
let!(:daily_rule_01) { IceT::Rule::Daily.new(1) }
let!(:daily_rule_02) { IceT::Rule::Daily.new(2) }

describe '#occurrences' do
it { expect(subject).to respond_to(:occurrences) }

context 'with one rule' do
coll = IceT::Collection.new
coll.add_rule IceT::Rule::Daily.new(1)
subject { coll.occurrences(2.weeks.ago, Time.now) }
it "does return an array" do
expect(subject).to be_a(Array)
end
its(:first) { should be_a(Time) }
end
context 'with two rules' do
coll = IceT::Collection.new
coll.add_rule IceT::Rule::Daily.new(1)
coll.add_rule IceT::Rule::Daily.new(2)
subject { coll.occurrences(2.weeks.ago, Time.now) }
it "does return an array" do
expect(subject).to be_a(Array)
end
its(:first) { should be_a(Time) }
end
end

describe '#add_rule' do
it { expect(subject).to respond_to(:add_rule) }

Expand Down Expand Up @@ -48,6 +72,7 @@
describe wday do
coll = IceT::Collection.new
coll.add_rule IceT::Rule::Daily.new(1)
coll.add_rule IceT::Rule::Daily.new(2)
times = coll.send(wday, 1.week.ago, Time.now)
times.each { |t|
meth = IceT::TimeHelper.question_mark_dayname(wday)
Expand Down

0 comments on commit 461d4a2

Please sign in to comment.