Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
rename #occurs_at? to #includes?
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Rogers committed May 9, 2016
1 parent 2c76268 commit a765944
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 64 deletions.
26 changes: 13 additions & 13 deletions README.md
Expand Up @@ -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)`).
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib/availability/abstract_availability.rb
Expand Up @@ -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
Expand All @@ -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? \
Expand Down
2 changes: 1 addition & 1 deletion lib/availability/hourly.rb
Expand Up @@ -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 }
Expand Down
84 changes: 42 additions & 42 deletions spec/availability_spec.rb
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -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

Expand All @@ -333,43 +333,43 @@
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

context 'when the availability is every 30 days and starts 11 days after the beginning' do
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

Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/hourly_spec.rb
Expand Up @@ -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),
Expand All @@ -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
Expand Down

0 comments on commit a765944

Please sign in to comment.