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

Add ability to intersect schedules #51

Merged
merged 8 commits into from
Mar 30, 2016
Merged

Conversation

craiglittle
Copy link
Collaborator

An intersection of two schedules consists of:

  • an intersection of intervals
  • a union of holidays
  • the time zone of the first schedule

For example:

schedule_1 = Biz::Schedule.new do |config|
  config.hours = {
    mon: {'09:00' => '17:00'},
    tue: {'10:00' => '16:00'},
    wed: {'09:00' => '17:00'},
    thu: {'10:00' => '16:00'},
    fri: {'09:00' => '17:00'},
    sat: {'11:00' => '14:30'}
  }

  config.holidays = [Date.new(2016, 1, 1), Date.new(2016, 12, 25)]

  config.time_zone = 'Etc/UTC'
end

schedule_2 = Biz::Schedule.new do |config|
  config.hours = {
    sun: {'10:00' => '12:00'},
    mon: {'08:00' => '10:00'},
    tue: {'11:00' => '15:00'},
    wed: {'16:00' => '18:00'},
    thu: {'11:00' => '12:00', '13:00' => '14:00'}
  }

  config.holidays = [
    Date.new(2016, 1, 1),
    Date.new(2016, 7, 4),
    Date.new(2016, 11, 24)
  ]

  config.time_zone = 'America/Los_Angeles'
end

schedule_1 & schedule_2

results in a schedule that is configured like:

Biz::Schedule.new do |config|
  config.hours = {
    mon: {'09:00' => '10:00'},
    tue: {'11:00' => '15:00'},
    wed: {'16:00' => '17:00'},
    thu: {'11:00' => '12:00', '13:00' => '14:00'}
  }

  config.holidays = [
    Date.new(2016, 1, 1),
    Date.new(2016, 7, 4),
    Date.new(2016, 11, 24),
    Date.new(2016, 12, 25)
  ]

  config.time_zone = 'Etc/UTC'
end

Closes #50.

@zendesk/darko

/cc @pocman

Since we need to interact with it directly to implement the schedule
intersection feature, we should be more safe.
Two tweaks:
  * Duplicate holidays will now be dropped
  * Holidays will now be returned in sorted order
Just a couple bits of tidying up for consistency.
Always be editing.
An intersection of two schedules consists of:
  * an intersection of intervals
  * a union of holidays
  * the time zone of the first schedule

See the `README` for an example.

Closes #50.
This is an interesting one.

It turns out when accessing protected methods on an object of the same
type through the use of `map`, an exception is raised on Ruby 2.2 and
lower.

This seems like a bug and appears to have been fixed in Ruby 2.3.

To avoid this issue, we'll just make the interval attributes public and
document them with tests.
Gotta stay current.
I think this is a bit more intentional-revealing and allows us to get
rid of the dependency on `set`.
@alex-stone
Copy link

👍 looks good to me! Union time?

@craiglittle craiglittle merged commit b659d17 into master Mar 30, 2016
@craiglittle craiglittle deleted the craig/schedule-intersection branch March 30, 2016 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants