Module: TimePieces::Duration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#duration_secondsObject

Returns the value of attribute duration_seconds.



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

def duration_seconds
  @duration_seconds
end

#start_at_secondsObject

Returns the value of attribute start_at_seconds.



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

def start_at_seconds
  @start_at_seconds
end

Instance Method Details

#+(other_td) ⇒ Object



63
64
65
66
67
68
69
70
71
72
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
# File 'lib/time_pieces/duration.rb', line 63

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/time_pieces/duration.rb', line 103

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



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

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

#end_at_secondsObject



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

def end_at_seconds
  return start_at_seconds + duration_seconds
end

#end_at_seconds=(new_end_at_seconds) ⇒ Object



7
8
9
# File 'lib/time_pieces/duration.rb', line 7

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

#inside_of?(other_td) ⇒ Boolean



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

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



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

def left_duration_copy
  duration_copy
end

#overlaps?(other_td) ⇒ Boolean



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

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



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

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



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

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



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

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

#overlaps_start?(other_td) ⇒ Boolean



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

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



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

def right_duration_copy
  duration_copy
end

#touches?(other_td) ⇒ Boolean



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

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

#touches_at_end?(other_td) ⇒ Boolean



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

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



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

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



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

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