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
- #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 |
.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
42 43 44 45 46 47 |
# File 'lib/biz/time_segment.rb', line 42 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) end |
#/(other) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/biz/time_segment.rb', line 49 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
38 39 40 |
# File 'lib/biz/time_segment.rb', line 38 def contains?(time) (start_time...end_time).cover?(time) end |
#duration ⇒ Object
26 27 28 |
# File 'lib/biz/time_segment.rb', line 26 def duration Duration.new(end_time - start_time) 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 |