class Biz::Interval

Attributes

end_time[R]
start_time[R]
time_zone[R]

Public Class Methods

new(start_time, end_time, time_zone) click to toggle source
# File lib/biz/interval.rb, line 18
def initialize(start_time, end_time, time_zone)
  @start_time = start_time
  @end_time   = end_time
  @time_zone  = time_zone
end
to_hours(intervals) click to toggle source
# File lib/biz/interval.rb, line 10
def self.to_hours(intervals)
  intervals.each_with_object(
    Hash.new do |hours, wday| hours.store(wday, {}) end
  ) do |interval, hours|
    hours[interval.wday_symbol].store(*interval.endpoints.map(&:timestamp))
  end
end

Public Instance Methods

&(other) click to toggle source
# File lib/biz/interval.rb, line 52
def &(other)
  lower_bound = [self, other].map(&:start_time).max
  upper_bound = [self, other].map(&:end_time).min

  self.class.new(lower_bound, [lower_bound, upper_bound].max, time_zone)
end
contains?(time) click to toggle source
# File lib/biz/interval.rb, line 38
def contains?(time)
  (start_time...end_time).cover?(
    WeekTime.from_time(Time.new(time_zone).local(time))
  )
end
empty?() click to toggle source
# File lib/biz/interval.rb, line 34
def empty?
  start_time >= end_time
end
endpoints() click to toggle source
# File lib/biz/interval.rb, line 30
def endpoints
  [start_time, end_time]
end
to_time_segment(week) click to toggle source
# File lib/biz/interval.rb, line 44
def to_time_segment(week)
  TimeSegment.new(
    *endpoints.map { |endpoint|
      Time.new(time_zone).during_week(week, endpoint)
    }
  )
end

Private Instance Methods

<=>(other) click to toggle source
# File lib/biz/interval.rb, line 61
def <=>(other)
  return unless other.is_a?(self.class)

  [start_time, end_time, time_zone] <=>
    [other.start_time, other.end_time, other.time_zone]
end