From a7659445d8b1d858e4ffdfe26d8cdf3472688231 Mon Sep 17 00:00:00 2001 From: Jason Rogers Date: Mon, 9 May 2016 13:05:08 -0400 Subject: [PATCH] rename #occurs_at? to #includes? --- README.md | 26 +++---- lib/availability/abstract_availability.rb | 10 +-- lib/availability/hourly.rb | 2 +- spec/availability_spec.rb | 84 +++++++++++------------ spec/hourly_spec.rb | 6 +- 5 files changed, 64 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 1d0022f..ad62b06 100644 --- a/README.md +++ b/README.md @@ -42,23 +42,23 @@ This returns a single time object for the next occurrence on or after the given ```ruby # Every other Monday from 9:00 AM to 10:00 AM starting on May 2, 2016 every_other_monday = Availability.every_other_week(start_time: Time.new(2016, 5, 2, 9), duration: 1.hour) -every_other_monday.occurs_at? Time.new(2016, 5, 30, 9) # => true -every_other_monday.occurs_at? Time.new(2016, 5, 30, 10) # => false, because it lasts only an hour -every_other_monday.occurs_at? Time.new(2016, 5, 23, 9) # => false, because it's not a covered Monday -every_other_monday.occurs_at? Time.new(2016, 5, 18, 9) # => false, because it's not a Monday +every_other_monday.includes? Time.new(2016, 5, 30, 9) # => true +every_other_monday.includes? Time.new(2016, 5, 30, 10) # => false, because it lasts only an hour +every_other_monday.includes? Time.new(2016, 5, 23, 9) # => false, because it's not a covered Monday +every_other_monday.includes? Time.new(2016, 5, 18, 9) # => false, because it's not a Monday # A business week starting on May 2, 2016 going from 1:30 PM until 2:00 PM every day biz_week = Availability.daily(start_time: Time.new(2016, 5, 2, 13, 30), stops_by: Time.new(2016, 5, 6), duration: 30.minutes) -biz_week.occurs_at? Time.new(2016, 5, 3, 13, 30) #=> true -biz_week.occurs_at? Time.new(2016, 5, 3, 14, 30) #=> false -biz_week.occurs_at? Time.new(2016, 5, 6, 13, 30) #=> true +biz_week.includes? Time.new(2016, 5, 3, 13, 30) #=> true +biz_week.includes? Time.new(2016, 5, 3, 14, 30) #=> false +biz_week.includes? Time.new(2016, 5, 6, 13, 30) #=> true # A semi-monthly availability occurring all day, without an end every_other_month = Availability.every_other_month(start_time: Time.new(2016, 1, 1), duration: 1.day) -every_other_month.occurs_at? Time.new(2016, 3, 1) #=> true -every_other_month.occurs_at? Time.new(4037, 7, 1) #=> true +every_other_month.includes? Time.new(2016, 3, 1) #=> true +every_other_month.includes? Time.new(4037, 7, 1) #=> true ``` Exclusion rules can be added to an availability to further restrict it. For instance, if you wanted to create an availability for business days that spanned more than a single week you might do something like the following (note that exclusion rules need only to respond to `violated_by?(time)`). @@ -85,10 +85,10 @@ business_days = Availability.daily( exclusions: [Availability::Exclusion.new(BusinessDayRule.new)] ) -business_days.occurs_at? Time.new(2016, 5, 2, 8) #=> true -business_days.occurs_at? Time.new(2016, 5, 2, 10) #=> true -business_days.occurs_at? Time.new(2016, 5, 2, 7) #=> false -business_days.occurs_at? Time.new(2016, 5, 2, 18) #=> false +business_days.includes? Time.new(2016, 5, 2, 8) #=> true +business_days.includes? Time.new(2016, 5, 2, 10) #=> true +business_days.includes? Time.new(2016, 5, 2, 7) #=> false +business_days.includes? Time.new(2016, 5, 2, 18) #=> false ``` ## TODO diff --git a/lib/availability/abstract_availability.rb b/lib/availability/abstract_availability.rb index 6b18c01..5777a45 100644 --- a/lib/availability/abstract_availability.rb +++ b/lib/availability/abstract_availability.rb @@ -51,13 +51,13 @@ def beginning # @return [Boolean] true or false # def corresponds_to?(availability) - return false unless occurs_at?(availability.start_time) \ - && occurs_at?(availability.start_time + availability.duration - 1.second) + return false unless includes?(availability.start_time) \ + && includes?(availability.start_time + availability.duration - 1.second) if !!stops_by that_last = availability.last_occurrence !that_last.nil? \ - && occurs_at?(that_last) \ - && occurs_at?(that_last + availability.duration - 1.second) \ + && includes?(that_last) \ + && includes?(that_last + availability.duration - 1.second) \ && that_last.to_date <= self.last_occurrence.to_date else true @@ -71,7 +71,7 @@ def corresponds_to?(availability) # # @return [Boolean] true or false # - def occurs_at?(time) + def includes?(time) next_occurrence = next_occurrence(time) || last_occurrence residue_for(time) == @residue \ && !next_occurrence.nil? \ diff --git a/lib/availability/hourly.rb b/lib/availability/hourly.rb index 0127daa..46f91aa 100644 --- a/lib/availability/hourly.rb +++ b/lib/availability/hourly.rb @@ -14,7 +14,7 @@ def move_by(time, amount) time + amount.hours end - def occurs_at?(time) + def includes?(time) return true if super return false if residue_for(time) != residue hours_on_same_day = next_n_occurrences(24, time).select {|t| t.wday == time.wday && t <= time } diff --git a/spec/availability_spec.rb b/spec/availability_spec.rb index 7c4ec24..1891d97 100644 --- a/spec/availability_spec.rb +++ b/spec/availability_spec.rb @@ -226,7 +226,7 @@ it 'stops after one occurrence' do availability = Availability.once start_time: Date.today, duration: 30.minutes expect(availability.last_occurrence).to eq Date.today.to_time - expect(availability.occurs_at? availability.start_time).to be_truthy + expect(availability.includes? availability.start_time).to be_truthy end end @@ -238,8 +238,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -250,8 +250,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -262,8 +262,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -274,8 +274,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -286,8 +286,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -298,8 +298,8 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end @@ -310,21 +310,21 @@ its(:last_occurrence) { should_not be_nil } its(:last_occurrence) { should eq expected_last_occurrence } - it 'expects occurs_at?(last_occurrence) to be true' do - expect(subject.occurs_at? subject.last_occurrence).to be_truthy + it 'expects includes?(last_occurrence) to be true' do + expect(subject.includes? subject.last_occurrence).to be_truthy end end end end - describe '#occurs_at?' do + describe '#includes?' do context 'comparing times' do let(:every_other_monday) do Availability.every_other_week(start_time: Time.new(2016, 5, 2, 9), duration: 1.hour) end it 'should not include the start of the following hour for a one-hour availability' do - expect(every_other_monday.occurs_at? Time.new(2016, 5, 30, 10)).to be_falsey + expect(every_other_monday.includes? Time.new(2016, 5, 30, 10)).to be_falsey end end @@ -333,27 +333,27 @@ let(:availability) { Availability.create(duration: 45.minutes, interval: 14, start_time: beginning + 5.days) } it 'occurs on its start date' do - expect(availability.occurs_at?(beginning + 5.days)).to be_truthy + expect(availability.includes?(beginning + 5.days)).to be_truthy end it 'occurs on its next occurrence' do - expect(availability.occurs_at?(beginning + 5.days + 14.days)).to be_truthy + expect(availability.includes?(beginning + 5.days + 14.days)).to be_truthy end it 'does not occur on the day after its next occurrence' do - expect(availability.occurs_at?(beginning + 5.days + 15.days)).to be_falsey + expect(availability.includes?(beginning + 5.days + 15.days)).to be_falsey end it 'occurs during the duration' do - expect(availability.occurs_at?(beginning + 5.days + 15.minutes)).to be_truthy + expect(availability.includes?(beginning + 5.days + 15.minutes)).to be_truthy end it 'does not occur before the duration' do - expect(availability.occurs_at?(beginning + 5.days - 1.minute)).to be_falsey + expect(availability.includes?(beginning + 5.days - 1.minute)).to be_falsey end it 'does not occur after the duration' do - expect(availability.occurs_at?(beginning + 5.days + 46.minutes)).to be_falsey + expect(availability.includes?(beginning + 5.days + 46.minutes)).to be_falsey end end @@ -361,15 +361,15 @@ let(:availability) { Availability.create(duration: 45.minutes, interval: 30, start_time: beginning + 11.days) } it 'occurs on its start date' do - expect(availability.occurs_at?(beginning + 11.days)).to be_truthy + expect(availability.includes?(beginning + 11.days)).to be_truthy end it 'occurs on its next occurrence' do - expect(availability.occurs_at?(beginning + 11.days + 30.days)).to be_truthy + expect(availability.includes?(beginning + 11.days + 30.days)).to be_truthy end it 'does not occur on the day before its next occurrence' do - expect(availability.occurs_at?(beginning + 11.days + 29.days)).to be_falsey + expect(availability.includes?(beginning + 11.days + 29.days)).to be_falsey end end @@ -381,10 +381,10 @@ common_date = Time.new(1970, 2, 20) - expect(availability_1.occurs_at?(common_date)).to be_truthy - expect(availability_2.occurs_at?(common_date)).to be_truthy - expect(availability_3.occurs_at?(common_date)).to be_truthy - expect(availability_4.occurs_at?(common_date)).to be_falsey + expect(availability_1.includes?(common_date)).to be_truthy + expect(availability_2.includes?(common_date)).to be_truthy + expect(availability_3.includes?(common_date)).to be_truthy + expect(availability_4.includes?(common_date)).to be_falsey end end @@ -394,16 +394,16 @@ let(:availability) { Availability.weekly(duration: 45.minutes, start_time: start_time) } it 'occurs on its start date' do - expect(availability.occurs_at?(start_time)).to be_truthy + expect(availability.includes?(start_time)).to be_truthy end it 'occurs on its next occurrence' do - expect(availability.occurs_at?(start_time + 1.week)).to be_truthy + expect(availability.includes?(start_time + 1.week)).to be_truthy end it 'does not occur on another day of the week' do (1..6).each do |i| - expect(availability.occurs_at?(start_time + i.days)).to be_falsey + expect(availability.includes?(start_time + i.days)).to be_falsey end end end @@ -414,15 +414,15 @@ let(:availability) { Availability.create(duration: 45.minutes, frequency: :monthly, interval: 3, start_time: beginning + 2.months) } it 'occurs on its start date' do - expect(availability.occurs_at?(beginning + 2.months)).to be_truthy + expect(availability.includes?(beginning + 2.months)).to be_truthy end it 'occurs on its next occurrence' do - expect(availability.occurs_at?(beginning + 2.months + 3.months)).to be_truthy + expect(availability.includes?(beginning + 2.months + 3.months)).to be_truthy end it 'does not occur on the month after its next occurrence' do - expect(availability.occurs_at?(beginning + 2.months + 4.months)).to be_falsey + expect(availability.includes?(beginning + 2.months + 4.months)).to be_falsey end end end @@ -432,15 +432,15 @@ let(:availability) { Availability.create(duration: 2.hours, frequency: :yearly, interval: 2, start_time: beginning + 1.year) } it 'occurs on its start date' do - expect(availability.occurs_at?(beginning + 1.year)).to be_truthy + expect(availability.includes?(beginning + 1.year)).to be_truthy end it 'occurs on its next occurrence' do - expect(availability.occurs_at?(beginning + 1.year + 2.years)).to be_truthy + expect(availability.includes?(beginning + 1.year + 2.years)).to be_truthy end it 'does not occur on the month after its next occurrence' do - expect(availability.occurs_at?(beginning + 1.year + 3.years)).to be_falsey + expect(availability.includes?(beginning + 1.year + 3.years)).to be_falsey end end end @@ -597,15 +597,15 @@ let(:after_time) { Time.new(2091, 8, 13, 19) } it 'occurs during normal business hours' do - expect(business_days.occurs_at? monday_at_10_am).to be_truthy + expect(business_days.includes? monday_at_10_am).to be_truthy end it 'does not occur before normal business hours' do - expect(business_days.occurs_at? before_time).to be_falsey + expect(business_days.includes? before_time).to be_falsey end it 'does not occur after normal business hours' do - expect(business_days.occurs_at? after_time).to be_falsey + expect(business_days.includes? after_time).to be_falsey end end end diff --git a/spec/hourly_spec.rb b/spec/hourly_spec.rb index 405c9a3..4c41ece 100644 --- a/spec/hourly_spec.rb +++ b/spec/hourly_spec.rb @@ -81,7 +81,7 @@ end end - describe '#occurs_at?' do + describe '#includes?' do let(:availabilities) {[ Availability.hourly(duration: 90.minutes, interval: 2, start_time: beginning), Availability.hourly(duration: 90.minutes, interval: 2, start_time: beginning + 2.hours), @@ -94,14 +94,14 @@ context 'but would finish on time' do it 'occurs' do (1..15).each do |offset| - expect(availabilities.first.occurs_at?(beginning + offset.minutes)).to be_truthy + expect(availabilities.first.includes?(beginning + offset.minutes)).to be_truthy end end end context 'and would not finish on time' do it 'occurs' do - expect(availabilities.first.occurs_at?(beginning + 16.minutes)).to be_truthy + expect(availabilities.first.includes?(beginning + 16.minutes)).to be_truthy end end end