Skip to content

Commit

Permalink
Merge pull request #58 from zendesk/craig/intersection-complexity
Browse files Browse the repository at this point in the history
Lower complexity of `Schedule#&` method
  • Loading branch information
craiglittle committed Apr 8, 2016
2 parents acb0909 + 0368627 commit 155acf4
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/biz/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,8 @@ def in_zone

def &(other)
self.class.new do |config|
config.hours = Interval.to_hours(
intervals.flat_map { |interval|
other
.intervals
.map { |other_interval| interval & other_interval }
.reject(&:empty?)
}
)

config.holidays = [*holidays, *other.holidays].map(&:to_date)

config.hours = Interval.to_hours(intersected_intervals(other))
config.holidays = [*holidays, *other.holidays].map(&:to_date)
config.time_zone = time_zone.name
end
end
Expand All @@ -67,5 +58,16 @@ def &(other)

attr_reader :configuration

private

def intersected_intervals(other)
intervals.flat_map { |interval|
other
.intervals
.map { |other_interval| interval & other_interval }
.reject(&:empty?)
}
end

end
end

0 comments on commit 155acf4

Please sign in to comment.