Class: TimeIntervals::Interval
- Inherits:
-
Object
- Object
- TimeIntervals::Interval
- Includes:
- Comparable
- Defined in:
- lib/time_intervals/interval.rb
Instance Attribute Summary collapse
-
#ended_at ⇒ Object
(also: #end_at)
readonly
Returns the value of attribute ended_at.
-
#started_at ⇒ Object
(also: #start_at)
readonly
Returns the value of attribute started_at.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #after?(other) ⇒ Boolean
- #before?(other) ⇒ Boolean
- #disjoint?(other) ⇒ Boolean
- #hash ⇒ Object
- #include?(time) ⇒ Boolean
-
#initialize(started_at, ended_at) ⇒ Interval
constructor
A new instance of Interval.
- #length_in_seconds ⇒ Object
- #overlap_duration_in_seconds(other) ⇒ Object
- #overlaps?(other) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(started_at, ended_at) ⇒ Interval
Returns a new instance of Interval.
23 24 25 26 27 28 29 30 |
# File 'lib/time_intervals/interval.rb', line 23 def initialize(started_at, ended_at) raise "Invalid interval" if started_at.nil? || ended_at.nil? @started_at = as_seconds(started_at) @ended_at = as_seconds(ended_at) raise "Invalid interval: #{self}" if @ended_at < @started_at end |
Instance Attribute Details
#ended_at ⇒ Object (readonly) Also known as: end_at
Returns the value of attribute ended_at.
15 16 17 |
# File 'lib/time_intervals/interval.rb', line 15 def ended_at @ended_at end |
#started_at ⇒ Object (readonly) Also known as: start_at
Returns the value of attribute started_at.
15 16 17 |
# File 'lib/time_intervals/interval.rb', line 15 def started_at @started_at end |
Class Method Details
.create(interval) ⇒ Object
19 20 21 |
# File 'lib/time_intervals/interval.rb', line 19 def self.create(interval) new(interval.started_at, interval.ended_at) end |
Instance Method Details
#<=>(other) ⇒ Object
65 66 67 68 |
# File 'lib/time_intervals/interval.rb', line 65 def <=>(other) comparison = started_at <=> other.started_at comparison.zero? ? (other.ended_at <=> ended_at) : comparison end |
#==(other) ⇒ Object Also known as: eql?
70 71 72 |
# File 'lib/time_intervals/interval.rb', line 70 def ==(other) other.class == self.class && other.state == state end |
#after?(other) ⇒ Boolean
36 37 38 |
# File 'lib/time_intervals/interval.rb', line 36 def after?(other) other.ended_at <= started_at end |
#before?(other) ⇒ Boolean
40 41 42 |
# File 'lib/time_intervals/interval.rb', line 40 def before?(other) ended_at <= other.started_at end |
#disjoint?(other) ⇒ Boolean
44 45 46 |
# File 'lib/time_intervals/interval.rb', line 44 def disjoint?(other) before?(other) || after?(other) end |
#hash ⇒ Object
75 76 77 |
# File 'lib/time_intervals/interval.rb', line 75 def hash state.hash end |
#include?(time) ⇒ Boolean
57 58 59 |
# File 'lib/time_intervals/interval.rb', line 57 def include?(time) started_at <= time && time < ended_at end |
#length_in_seconds ⇒ Object
32 33 34 |
# File 'lib/time_intervals/interval.rb', line 32 def length_in_seconds ended_at - started_at end |
#overlap_duration_in_seconds(other) ⇒ Object
52 53 54 55 |
# File 'lib/time_intervals/interval.rb', line 52 def overlap_duration_in_seconds(other) return 0 if disjoint?(other) [other.ended_at, ended_at].min - [other.started_at, started_at].max end |
#overlaps?(other) ⇒ Boolean
48 49 50 |
# File 'lib/time_intervals/interval.rb', line 48 def overlaps?(other) !disjoint?(other) end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/time_intervals/interval.rb', line 61 def to_s "[#{format(started_at)}, #{format(ended_at)}]" end |