Class: Timerage::TimeInterval

Inherits:
Range
  • Object
show all
Defined in:
lib/timerage/time_interval.rb

Overview

A range of time. The exposes the Range like interface.

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TimeInterval

Returns a new instance of TimeInterval.



6
7
8
9
10
11
12
13
14
# File 'lib/timerage/time_interval.rb', line 6

def initialize(*args)
  rng = if rangeish?(args.first)
          args.first
        else
          Range.new(*args)
        end

  super rng
end

Instance Method Details

#+(other) ⇒ Object

Return new TimeInterval that is the concatenation of self and other (if possible).

Raises ArgumentError if other is not adjacent to self chronologically.



31
32
33
34
35
# File 'lib/timerage/time_interval.rb', line 31

def +(other)
  fail ArgumentError, "other must be adjacent to self" unless adjacent_to?(other)

  self.class.new([self.begin, other.begin].min, [self.end, other.end].max)
end

#adjacent_to?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/timerage/time_interval.rb', line 43

def adjacent_to?(other)
  other.begin == self.end || other.end == self.begin
end

#iso8601(*args) ⇒ Object

Returns an ISO8601 interval representation of self Takes same args as Time#iso8601



39
40
41
# File 'lib/timerage/time_interval.rb', line 39

def iso8601(*args)
  "#{self.begin.iso8601(*args)}/#{self.end.iso8601(*args)}"
end

#step(n, &blk) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/timerage/time_interval.rb', line 18

def step(n, &blk)
  if block_given?
    time_enumerator(n).each(&blk)
  else
    time_enumerator(n)
  end
end