Class: Linesmen::Timeline
- Inherits:
-
Object
- Object
- Linesmen::Timeline
- Defined in:
- lib/linesmen/timeline.rb
Overview
This class deals with timeline operations. We understand timelines here as a sequence of non-contiguous events. The methods here are mostly boolean operations. This article may help you to understand them: www.wikiwand.com/en/Boolean_operations_on_polygons
Instance Attribute Summary collapse
-
#time_ranges ⇒ Object
readonly
Returns the value of attribute time_ranges.
Instance Method Summary collapse
- #gross_duration ⇒ Object
- #hours ⇒ Object
-
#initialize(*time_ranges) ⇒ Timeline
constructor
A new instance of Timeline.
- #intersect(other) ⇒ Object (also: #&)
- #invert ⇒ Object (also: #!@)
- #minutes ⇒ Object
- #net_duration ⇒ Object (also: #seconds)
- #subtract(other) ⇒ Object (also: #-)
- #union(other) ⇒ Object (also: #+)
Constructor Details
#initialize(*time_ranges) ⇒ Timeline
Returns a new instance of Timeline.
11 12 13 14 |
# File 'lib/linesmen/timeline.rb', line 11 def initialize(*time_ranges) @time_ranges = time_ranges ensure_no_overlapping! end |
Instance Attribute Details
#time_ranges ⇒ Object (readonly)
Returns the value of attribute time_ranges.
9 10 11 |
# File 'lib/linesmen/timeline.rb', line 9 def time_ranges @time_ranges end |
Instance Method Details
#gross_duration ⇒ Object
54 55 56 |
# File 'lib/linesmen/timeline.rb', line 54 def gross_duration time_ranges.last.end - time_ranges.first.begin end |
#hours ⇒ Object
70 71 72 |
# File 'lib/linesmen/timeline.rb', line 70 def hours minutes / 60 end |
#intersect(other) ⇒ Object Also known as: &
34 35 36 37 38 39 |
# File 'lib/linesmen/timeline.rb', line 34 def intersect(other) case other when Range then intersect_time_ranges(other) when Timeline then intersect_time_ranges(*other.time_ranges) end end |
#invert ⇒ Object Also known as: !@
43 44 45 46 47 48 49 50 |
# File 'lib/linesmen/timeline.rb', line 43 def invert Timeline.new( *time_ranges.map.with_index do |range, index| next if index.zero? time_ranges[index - 1].end..range.begin end.compact ) end |
#minutes ⇒ Object
66 67 68 |
# File 'lib/linesmen/timeline.rb', line 66 def minutes seconds / 60 end |
#net_duration ⇒ Object Also known as: seconds
58 59 60 61 62 |
# File 'lib/linesmen/timeline.rb', line 58 def net_duration time_ranges.sum(0) do |time_range| time_range.end - time_range.begin end end |
#subtract(other) ⇒ Object Also known as: -
25 26 27 28 29 30 |
# File 'lib/linesmen/timeline.rb', line 25 def subtract(other) case other when Range then subtract_time_ranges(other) when Timeline then subtract_time_ranges(*other.time_ranges) end end |