Class: Biz::TimeSegment

Inherits:
Object
  • Object
show all
Defined in:
lib/biz/time_segment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time) ⇒ TimeSegment

Returns a new instance of TimeSegment.



17
18
19
20
# File 'lib/biz/time_segment.rb', line 17

def initialize(start_time, end_time)
  @start_time = start_time
  @end_time   = end_time
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



14
15
16
# File 'lib/biz/time_segment.rb', line 14

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



14
15
16
# File 'lib/biz/time_segment.rb', line 14

def start_time
  @start_time
end

Class Method Details

.after(time) ⇒ Object



10
11
12
# File 'lib/biz/time_segment.rb', line 10

def self.after(time)
  new(time, Time::HEAT_DEATH)
end

.before(time) ⇒ Object



6
7
8
# File 'lib/biz/time_segment.rb', line 6

def self.before(time)
  new(Time::BIG_BANG, time)
end

Instance Method Details

#&(other) ⇒ Object



38
39
40
41
42
43
# File 'lib/biz/time_segment.rb', line 38

def &(other)
  self.class.new(
    lower_bound(other),
    [lower_bound(other), upper_bound(other)].max
  )
end

#/(other) ⇒ Object



45
46
47
48
49
50
# File 'lib/biz/time_segment.rb', line 45

def /(other)
  [
    self.class.new(start_time, other.start_time),
    self.class.new(other.end_time, end_time)
  ].reject(&:empty?).map { |potential| self & potential }
end

#contains?(time) ⇒ Boolean

Returns:

  • (Boolean)


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

def contains?(time)
  (start_time..end_time).cover?(time)
end

#durationObject



22
23
24
# File 'lib/biz/time_segment.rb', line 22

def duration
  Duration.new(end_time - start_time)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  start_time >= end_time
end

#endpointsObject



26
27
28
# File 'lib/biz/time_segment.rb', line 26

def endpoints
  [start_time, end_time]
end