Class: TimeSpan
- Inherits:
-
Object
- Object
- TimeSpan
- Defined in:
- lib/time_span.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#end_at ⇒ Object
Returns the value of attribute end_at.
-
#start_at ⇒ Object
Returns the value of attribute start_at.
Instance Method Summary collapse
-
#enveloped_by?(args, include_end = true) ⇒ Boolean
TODO.
- #envelopes?(t, include_end = true) ⇒ Boolean
- #first ⇒ Object
-
#initialize(args = {}) ⇒ TimeSpan
constructor
A new instance of TimeSpan.
- #last ⇒ Object
- #overlap(t, include_end = true) ⇒ Object
-
#overlap_by(t, include_end = true) ⇒ Object
ratio: overlap/duration.
- #overlaps?(t, include_end = true) ⇒ Boolean
Constructor Details
#initialize(args = {}) ⇒ TimeSpan
Returns a new instance of TimeSpan.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/time_span.rb', line 6 def initialize(args={}) @start_at = args[:start_at] if @end_at = args[:end_at] @end_at, @start_at = @start_at, @end_at if @start_at > @end_at @duration = @end_at - @start_at elsif @duration = args[:duration] @end_at = @start_at + @duration end end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
4 5 6 |
# File 'lib/time_span.rb', line 4 def duration @duration end |
#end_at ⇒ Object
Returns the value of attribute end_at.
4 5 6 |
# File 'lib/time_span.rb', line 4 def end_at @end_at end |
#start_at ⇒ Object
Returns the value of attribute start_at.
4 5 6 |
# File 'lib/time_span.rb', line 4 def start_at @start_at end |
Instance Method Details
#enveloped_by?(args, include_end = true) ⇒ Boolean
TODO
30 31 32 33 34 35 36 |
# File 'lib/time_span.rb', line 30 def enveloped_by?(args, include_end=true) case when args.is_a?(TimeSpan) when args.is_a?(Range) when args.is_a?(Array) end end |
#envelopes?(t, include_end = true) ⇒ Boolean
25 26 27 |
# File 'lib/time_span.rb', line 25 def envelopes?(t, include_end=true) t >= @start_at && ( t < @end_at || (include_end && t <= @end_at) ) end |
#first ⇒ Object
17 18 19 |
# File 'lib/time_span.rb', line 17 def first @start_at end |
#last ⇒ Object
21 22 23 |
# File 'lib/time_span.rb', line 21 def last @end_at end |
#overlap(t, include_end = true) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/time_span.rb', line 51 def overlap(t, include_end=true) return 0 unless self.overlaps?(t, include_end) e = t.end_at > @end_at ? @end_at : t.end_at s = t.start_at < @start_at ? @start_at : t.start_at o = e-s !include_end && o <= 1 ? 0.0 : o end |
#overlap_by(t, include_end = true) ⇒ Object
ratio: overlap/duration
47 48 49 |
# File 'lib/time_span.rb', line 47 def overlap_by(t, include_end=true) self.overlap(t, include_end)/@duration.to_f end |
#overlaps?(t, include_end = true) ⇒ Boolean
38 39 40 41 42 43 44 |
# File 'lib/time_span.rb', line 38 def overlaps?(t, include_end=true) if include_end t.end_at >= @start_at && t.start_at <= @end_at else t.end_at > @start_at && t.start_at < @end_at end end |