Class: Artic::TimeRange
- Inherits:
-
Range
- Object
- Range
- Artic::TimeRange
- Defined in:
- lib/artic/time_range.rb
Overview
Represents a range of two times in the same day (e.g. 09:00-17:00).
Constant Summary collapse
- TIME_REGEX =
/\A^(0\d|1\d|2[0-3]):[0-5]\d$\z/
Class Method Summary collapse
-
.build(range) ⇒ TimeRange
Builds a time range from the provided value.
Instance Method Summary collapse
-
#bisect(other) ⇒ Array<TimeRange>
Uses this range to bisect another.
-
#covers?(other) ⇒ Boolean
Returns whether this range completely covers the one given.
-
#initialize(start_time, end_time) ⇒ TimeRange
constructor
Initializes a new range.
-
#overlaps?(other) ⇒ Boolean
Returns whether the two ranges overlap (i.e. whether there’s at least a moment in time that belongs to both ranges).
-
#with_date(date) ⇒ Range
Returns a range of
Time
objects for this time range.
Constructor Details
#initialize(start_time, end_time) ⇒ TimeRange
Initializes a new range.
29 30 31 32 |
# File 'lib/artic/time_range.rb', line 29 def initialize(start_time, end_time) super validate_range end |
Class Method Details
Instance Method Details
#bisect(other) ⇒ Array<TimeRange>
Uses this range to bisect another.
If this range does not overlap the other, returns an array with the original range.
If this range completely covers the other, returns an empty array.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/artic/time_range.rb', line 98 def bisect(other) return [other] unless overlaps?(other) return [] if covers?(other) if self.begin <= other.begin && self.end <= other.end [self.end..other.end] elsif self.begin >= other.begin && self.end <= other.end [ (other.begin..self.begin), (self.end..other.end) ] elsif self.begin >= other.begin && self.end >= other.end [other.begin..self.begin] end end |
#covers?(other) ⇒ Boolean
Returns whether this range completely covers the one given.
49 50 51 |
# File 'lib/artic/time_range.rb', line 49 def covers?(other) self.begin <= other.begin && self.end >= other.end end |
#overlaps?(other) ⇒ Boolean
Returns whether the two ranges overlap (i.e. whether there’s at least a moment in time that belongs to both ranges).
40 41 42 |
# File 'lib/artic/time_range.rb', line 40 def overlaps?(other) self.begin <= other.end && self.end >= other.begin end |
#with_date(date) ⇒ Range
Returns a range of Time
objects for this time range.
58 59 60 61 62 63 |
# File 'lib/artic/time_range.rb', line 58 def with_date(date) Range.new( Time.parse("#{date} #{self.begin}"), Time.parse("#{date} #{self.end}") ) end |