Class: TimePieces::TimeSet

Inherits:
Object
  • Object
show all
Defined in:
lib/time_pieces/time_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimeSet

Returns a new instance of TimeSet.


4
5
6
# File 'lib/time_pieces/time_set.rb', line 4

def initialize
  @time_durations = []
end

Instance Attribute Details

#time_durationsObject

Returns the value of attribute time_durations.


3
4
5
# File 'lib/time_pieces/time_set.rb', line 3

def time_durations
  @time_durations
end

Instance Method Details

#+(other_ts) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/time_pieces/time_set.rb', line 46

def +(other_ts)
  ret_ts = TimeSet.new
  ret_ts.time_durations = time_durations.map{|td| td}
  other_ts.time_durations.each do |td|
    ret_ts << td
  end
  return ret_ts
end

#-(other_ts) ⇒ Object


54
55
56
57
58
59
60
61
62
# File 'lib/time_pieces/time_set.rb', line 54

def -(other_ts)
  ret_ts = TimeSet.new
  ret_ts.time_durations = Array.new(time_durations)
  other_ts.time_durations.each do |td|
    ret_ts >> td
  end
  return ret_ts
  
end

#<<(new_td) ⇒ Object

Adds a new time duration. Removes invalid time durations.


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/time_pieces/time_set.rb', line 8

def <<(new_td)
  if new_td.duration_seconds == 0
    
  else
    new_tds = []
    has_combined = false
    time_durations.each do |td|
      if new_td.overlaps?(td) || new_td.touches?(td)
        combined_td = td + new_td
        combined_td.time_durations.each do |td3|
          new_tds << td3
        end
        has_combined = true
      else
        new_tds << td
      end
    end
    new_tds << new_td unless has_combined
    self.time_durations = new_tds
  end
  return self
end

#>>(subtracting_td) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/time_pieces/time_set.rb', line 30

def >>(subtracting_td)
  new_tds = []
  time_durations.each do |td|
    if subtracting_td.overlaps?(td)
      combined_td = td - subtracting_td
      combined_td.time_durations.each do |td3|
        new_tds << td3
      end
    else
      new_tds << td
    end

  end
  self.time_durations = new_tds
  return self
end