Class: Biz::Interval

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/biz/interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time, time_zone) ⇒ Interval

Returns a new instance of Interval.



18
19
20
21
22
# 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

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



24
25
26
# File 'lib/biz/interval.rb', line 24

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



24
25
26
# File 'lib/biz/interval.rb', line 24

def start_time
  @start_time
end

#time_zoneObject (readonly)

Returns the value of attribute time_zone.



24
25
26
# File 'lib/biz/interval.rb', line 24

def time_zone
  @time_zone
end

Class Method Details

.to_hours(intervals) ⇒ Object



10
11
12
13
14
15
16
# 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

Instance Method Details

#&(other) ⇒ Object



52
53
54
55
56
57
# 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) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/biz/interval.rb', line 34

def empty?
  start_time >= end_time
end

#endpointsObject



30
31
32
# File 'lib/biz/interval.rb', line 30

def endpoints
  [start_time, end_time]
end

#to_time_segment(week) ⇒ Object



44
45
46
47
48
49
50
# 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