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
Returns a new TimeInterval that is the intersection of ‘self` and `other`.
-
#+(other) ⇒ Object
Return new TimeInterval that is the concatenation of self and other (if possible).
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #adjacent_to?(other) ⇒ Boolean
- #cover?(time_or_interval) ⇒ Boolean
-
#duration ⇒ Object
Returns number of seconds in this interval.
-
#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
- #slice(seconds) ⇒ Object
- #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
Returns a new TimeInterval that is the intersection of ‘self` and `other`.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/timerage/time_interval.rb', line 105 def &(other) fail ArgumentError, "#{other} does not overlap #{self}" unless self.overlap? other new_begin = [self.begin, other.begin].max new_end,ex_end = if self.end > other.end [other.end, other.exclude_end?] elsif self.end < other.end [self.end, self.exclude_end?] elsif self.exclude_end? || other.exclude_end? [self.end, true] else [self.end, false] end self.class.new(new_begin, new_end, ex_end) end |
#+(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.
44 45 46 47 48 |
# File 'lib/timerage/time_interval.rb', line 44 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
89 90 91 92 93 |
# File 'lib/timerage/time_interval.rb', line 89 def <=>(other) return super unless rangeish?(other) self.begin <=> other.begin end |
#==(other) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/timerage/time_interval.rb', line 95 def ==(other) self.begin == other.begin && self.end == other.end && self.exclude_end? == other.exclude_end? rescue NoMethodError false end |
#adjacent_to?(other) ⇒ Boolean
56 57 58 |
# File 'lib/timerage/time_interval.rb', line 56 def adjacent_to?(other) other.begin == self.end || other.end == self.begin end |
#cover?(time_or_interval) ⇒ Boolean
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/timerage/time_interval.rb', line 60 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 |
#duration ⇒ Object
Returns number of seconds in this interval
19 20 21 |
# File 'lib/timerage/time_interval.rb', line 19 def duration self.end - self.begin end |
#iso8601(*args) ⇒ Object
Returns an ISO8601 interval representation of self Takes same args as Time#iso8601
52 53 54 |
# File 'lib/timerage/time_interval.rb', line 52 def iso8601(*args) "#{self.begin.iso8601(*args)}/#{self.end.iso8601(*args)}" end |
#overlap?(other) ⇒ Boolean
80 81 82 83 84 85 86 87 |
# File 'lib/timerage/time_interval.rb', line 80 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 |
#slice(seconds) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/timerage/time_interval.rb', line 31 def slice(seconds) time_enumerator(seconds) .map{|t| end_time = [t+seconds, self.end].min inclusive = (t == end_time || t+seconds > self.end) && !exclude_end? TimeInterval.new(t, end_time, !inclusive) } end |
#step(n, &blk) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/timerage/time_interval.rb', line 23 def step(n, &blk) if block_given? time_enumerator(n).each(&blk) else time_enumerator(n) end end |