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

#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



73
74
75
76
77
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
# File 'lib/time_pieces/duration.rb', line 73

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



113
114
115
116
117
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
# File 'lib/time_pieces/duration.rb', line 113

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



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

def <=>(other)
  start_at_seconds <=> other.start_at_seconds
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



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

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



51
52
53
54
55
56
57
# File 'lib/time_pieces/duration.rb', line 51

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



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

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



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

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



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

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

#overlaps_start?(other_td) ⇒ Boolean



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

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



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

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

#touches_at_end?(other_td) ⇒ Boolean



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

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



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

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