Class: TimeIntervals::Interval

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/time_intervals/interval.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (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_atObject (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

Returns:

  • (Boolean)


36
37
38
# File 'lib/time_intervals/interval.rb', line 36

def after?(other)
  other.ended_at <= started_at
end

#before?(other) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/time_intervals/interval.rb', line 40

def before?(other)
  ended_at <= other.started_at
end

#disjoint?(other) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/time_intervals/interval.rb', line 44

def disjoint?(other)
  before?(other) || after?(other)
end

#hashObject



75
76
77
# File 'lib/time_intervals/interval.rb', line 75

def hash
  state.hash
end

#include?(time) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/time_intervals/interval.rb', line 57

def include?(time)
  started_at <= time && time < ended_at
end

#length_in_secondsObject



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

Returns:

  • (Boolean)


48
49
50
# File 'lib/time_intervals/interval.rb', line 48

def overlaps?(other)
  !disjoint?(other)
end

#to_sObject



61
62
63
# File 'lib/time_intervals/interval.rb', line 61

def to_s
  "[#{format(started_at)}, #{format(ended_at)}]"
end