Class: Timerage::TimeInterval
- Inherits:
-
Range
- Object
- Range
- Timerage::TimeInterval
- Defined in:
- lib/timerage/time_interval.rb
Overview
A range of time. The exposes the Range like interface.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Return new TimeInterval that is the concatenation of self and other (if possible).
- #<=>(other) ⇒ Object
- #adjacent_to?(other) ⇒ Boolean
- #cover?(time_or_interval) ⇒ Boolean
-
#initialize(*args) ⇒ TimeInterval
constructor
A new instance of TimeInterval.
-
#iso8601(*args) ⇒ Object
Returns an ISO8601 interval representation of self Takes same args as Time#iso8601.
- #overlap?(other) ⇒ Boolean
- #step(n, &blk) ⇒ Object
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 |
#<=>(other) ⇒ Object
77 78 79 80 81 |
# File 'lib/timerage/time_interval.rb', line 77 def <=>(other) return super unless rangeish?(other) self.begin <=> other.begin end |
#adjacent_to?(other) ⇒ 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 |
#cover?(time_or_interval) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/timerage/time_interval.rb', line 48 def cover?(time_or_interval) return super unless rangeish?(time_or_interval) other = time_or_interval self.begin <= other.begin && if self.exclude_end? && other.exclude_end? self.end > other.begin && self.begin < other.end && other.end <= self.end elsif self.exclude_end? self.end > other.begin && self.begin <= other.end && other.end < self.end elsif other.exclude_end? self.end >= other.begin && self.begin < other.end && other.end <= self.end else self.end >= other.begin && self.begin <= other.end && other.end <= self.end end 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 |
#overlap?(other) ⇒ Boolean
68 69 70 71 72 73 74 75 |
# File 'lib/timerage/time_interval.rb', line 68 def overlap?(other) cover?(other) || other.cover?(self) || cover?(other.begin) || other.cover?(self.begin) || cover?(other.end) && (!other.exclude_end? || other.end != self.begin) || other.cover?(self.end) && (!self.exclude_end? || other.begin != self.end) 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 |