Module: TimePieces::Duration

Included in:
SimpleDuration
Defined in:
lib/time_pieces/duration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#duration_secondsObject

Returns the value of attribute duration_seconds.



10
11
12
# File 'lib/time_pieces/duration.rb', line 10

def duration_seconds
  @duration_seconds
end

#lane_countObject

Returns the value of attribute lane_count.



10
11
12
# File 'lib/time_pieces/duration.rb', line 10

def lane_count
  @lane_count
end

#lane_numberObject

Returns the value of attribute lane_number.



10
11
12
# File 'lib/time_pieces/duration.rb', line 10

def lane_number
  @lane_number
end

#start_at_secondsObject

Returns the value of attribute start_at_seconds.



10
11
12
# File 'lib/time_pieces/duration.rb', line 10

def start_at_seconds
  @start_at_seconds
end

Class Method Details

.included(base) ⇒ Object



11
12
13
# File 'lib/time_pieces/duration.rb', line 11

def self.included(base)
  base.extend(DurationClassMethods)
end

Instance Method Details

#+(other_td) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/time_pieces/duration.rb', line 78

def +(other_td)
  ts_ret = TimeSet.new
  if overlaps?(other_td)
    if overlaps_start?(other_td) && !overlaps_outside?(other_td)
      update_start_seconds_and_end_seconds(other_td.start_at_seconds, end_at_seconds)
      ts_ret << self
      return ts_ret
    end
    if overlaps_end?(other_td) && !overlaps_outside?(other_td)
      
      update_start_seconds_and_end_seconds(start_at_seconds, other_td.end_at_seconds)
      ts_ret << self
      return ts_ret
    end
    if overlaps_inside?(other_td)
      ts_ret << self
      return ts_ret
    end
    if overlaps_outside?(other_td)
      ts_ret << other_td
    end
    return ts_ret
  end
  if touches?(other_td)
    if touches_at_end?(other_td) && other_td.end_at_seconds > end_at_seconds
      update_start_seconds_and_end_seconds(start_at_seconds, other_td.end_at_seconds)
      ts_ret << self
      return ts_ret
    end
    if touches_at_start?(other_td) && other_td.start_at_seconds < start_at_seconds
      update_start_seconds_and_end_seconds(other_td.start_at_seconds, end_at_seconds)
      ts_ret << self
      return ts_ret
    end
    return false
  end
  ts_ret << self
  ts_ret << other_td
  return ts_ret
end

#-(other_td) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/time_pieces/duration.rb', line 118

def -(other_td)
  ts_ret = TimeSet.new
  return ts_ret if inside_of?(other_td)
  unless overlaps?(other_td)
    ts_ret << self
    return ts_ret
  end
  if overlaps?(other_td)

    if overlaps_start?(other_td)
      update_start_seconds_and_end_seconds(other_td.end_at_seconds, end_at_seconds)
      ts_ret << self
      return ts_ret
    end
    if overlaps_end?(other_td)
      puts inspect
      update_start_seconds_and_end_seconds(start_at_seconds, other_td.start_at_seconds)
      puts "RUNNING OVERLAPPING END"
      puts inspect
      ts_ret << self
      return ts_ret
    end
    if overlaps_inside?(other_td)
      left_of_break = left_duration_copy.update_start_seconds_and_end_seconds(start_at_seconds, other_td.start_at_seconds)
      right_of_break = right_duration_copy.update_start_seconds_and_end_seconds(other_td.end_at_seconds, end_at_seconds)
      ts_ret << left_of_break
      ts_ret << right_of_break
    end
    return ts_ret
  end
end

#<=>(other) ⇒ Object



75
76
77
# File 'lib/time_pieces/duration.rb', line 75

def <=>(other)
  start_at_seconds <=> other.start_at_seconds
end

#clip!(start_at_seconds, end_at_seconds) ⇒ Object



31
32
33
34
35
36
# File 'lib/time_pieces/duration.rb', line 31

def clip!(start_at_seconds, end_at_seconds)
  self.start_at_seconds = start_at_seconds if self.start_at_seconds < start_at_seconds 
    if self.start_at_seconds + self.duration_seconds > end_at_seconds
      self.duration_seconds = end_at_seconds - start_at_seconds
    end
end

#end_at_secondsObject



14
15
16
# File 'lib/time_pieces/duration.rb', line 14

def end_at_seconds
  return start_at_seconds + duration_seconds
end

#end_at_seconds=(new_end_at_seconds) ⇒ Object



17
18
19
# File 'lib/time_pieces/duration.rb', line 17

def end_at_seconds=(new_end_at_seconds)
  self.duration_seconds = new_end_at_seconds - start_at_seconds
end

#inside_of?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/time_pieces/duration.rb', line 50

def inside_of?(other_td)
  return true if (other_td.start_at_seconds < start_at_seconds) && (other_td.end_at_seconds > end_at_seconds)
end

#left_duration_copyObject



25
26
27
# File 'lib/time_pieces/duration.rb', line 25

def left_duration_copy
  duration_copy
end

#overlaps?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


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

def overlaps?(other_td)
  return true if overlaps_outside?(other_td)
  return true if overlaps_start?(other_td)
  return true if overlaps_end?(other_td)
  return true if overlaps_inside?(other_td)
  return false
end

#overlaps_end?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/time_pieces/duration.rb', line 42

def overlaps_end?(other_td)
  return true if (end_at_seconds <= other_td.end_at_seconds) && (end_at_seconds > other_td.start_at_seconds)
  return false
end

#overlaps_inside?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/time_pieces/duration.rb', line 46

def overlaps_inside?(other_td)
  return true if (other_td.end_at_seconds > start_at_seconds) && (other_td.end_at_seconds <= end_at_seconds) && (other_td.start_at_seconds >= start_at_seconds) && (other_td.start_at_seconds < end_at_seconds)
  return false
end

#overlaps_outside?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/time_pieces/duration.rb', line 53

def overlaps_outside?(other_td)
  return other_td.overlaps_inside?(self)
end

#overlaps_start?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/time_pieces/duration.rb', line 38

def overlaps_start?(other_td)
  return true if (start_at_seconds >= other_td.start_at_seconds) && (start_at_seconds < other_td.end_at_seconds)
  return false
end

#right_duration_copyObject



28
29
30
# File 'lib/time_pieces/duration.rb', line 28

def right_duration_copy
  duration_copy
end

#touches?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/time_pieces/duration.rb', line 71

def touches?(other_td)
  return (touches_at_start?(other_td) || touches_at_end?(other_td))
  return false
end

#touches_at_end?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/time_pieces/duration.rb', line 67

def touches_at_end?(other_td)
  return true if other_td.start_at_seconds == end_at_seconds
  return false
end

#touches_at_start?(other_td) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/time_pieces/duration.rb', line 63

def touches_at_start?(other_td)
  return true if other_td.end_at_seconds == start_at_seconds
  return false
end

#update_start_seconds_and_end_seconds(new_start_seconds, new_end_seconds) ⇒ Object



20
21
22
23
24
# File 'lib/time_pieces/duration.rb', line 20

def update_start_seconds_and_end_seconds(new_start_seconds, new_end_seconds)
  self.start_at_seconds = new_start_seconds
  self.end_at_seconds = new_end_seconds
  return self
end