Class: Biz::TimeSegment

Inherits:
Object
  • Object
show all
Includes:
Comparable
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.



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

def initialize(start_time, end_time)
  @start_time = start_time
  @end_time   = end_time
  @date       = start_time.to_date
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Class Method Details

.after(time) ⇒ Object



12
13
14
# File 'lib/biz/time_segment.rb', line 12

def self.after(time)
  new(time, Time.heat_death)
end

.before(time) ⇒ Object



8
9
10
# File 'lib/biz/time_segment.rb', line 8

def self.before(time)
  new(Time.big_bang, time)
end

Instance Method Details

#&(other) ⇒ Object



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

def &(other)
  self.class.new(
    [self, other].map(&:start_time).max,
    [self, other].map(&:end_time).min
  )
end

#/(other) ⇒ Object



53
54
55
56
57
58
# File 'lib/biz/time_segment.rb', line 53

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

#contains?(time) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/biz/time_segment.rb', line 42

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

#disjoint?Boolean

Returns:

  • (Boolean)


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

def disjoint?
  start_time > end_time
end

#durationObject



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

def duration
  Duration.new([end_time - start_time, 0].max)
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  start_time == end_time
end

#endpointsObject



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

def endpoints
  [start_time, end_time]
end