Class: Biz::TimeSegment
- Inherits:
-
Object
- Object
- Biz::TimeSegment
- Includes:
- Comparable
- Defined in:
- lib/biz/time_segment.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#end_time ⇒ Object
readonly
Returns the value of attribute end_time.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
Class Method Summary collapse
Instance Method Summary collapse
- #&(other) ⇒ Object
- #/(other) ⇒ Object
- #contains?(time) ⇒ Boolean
- #disjoint? ⇒ Boolean
- #duration ⇒ Object
- #empty? ⇒ Boolean
- #endpoints ⇒ Object
-
#initialize(start_time, end_time) ⇒ TimeSegment
constructor
A new instance of TimeSegment.
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
#date ⇒ Object (readonly)
Returns the value of attribute date.
22 23 24 |
# File 'lib/biz/time_segment.rb', line 22 def date @date end |
#end_time ⇒ Object (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_time ⇒ Object (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 |
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
42 43 44 |
# File 'lib/biz/time_segment.rb', line 42 def contains?(time) (start_time...end_time).cover?(time) end |
#disjoint? ⇒ Boolean
38 39 40 |
# File 'lib/biz/time_segment.rb', line 38 def disjoint? start_time > end_time end |
#duration ⇒ Object
26 27 28 |
# File 'lib/biz/time_segment.rb', line 26 def duration Duration.new([end_time - start_time, 0].max) end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/biz/time_segment.rb', line 34 def empty? start_time == end_time end |
#endpoints ⇒ Object
30 31 32 |
# File 'lib/biz/time_segment.rb', line 30 def endpoints [start_time, end_time] end |