Class: Linesmen::Timeline

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

Instance Method Summary collapse

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_rangesObject (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_durationObject



54
55
56
# File 'lib/linesmen/timeline.rb', line 54

def gross_duration
  time_ranges.last.end - time_ranges.first.begin
end

#hoursObject



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

#invertObject 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

#minutesObject



66
67
68
# File 'lib/linesmen/timeline.rb', line 66

def minutes
  seconds / 60
end

#net_durationObject 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

#union(other) ⇒ Object Also known as: +



16
17
18
19
20
21
# File 'lib/linesmen/timeline.rb', line 16

def union(other)
  case other
  when Range    then Timeline.new(*(time_ranges + [other]))
  when Timeline then Timeline.new(*(time_ranges + other.time_ranges))
  end
end