lib/timerage/time_interval.rb in timerage-2.0.0 vs lib/timerage/time_interval.rb in timerage-2.1.0
- old
+ new
@@ -1,18 +1,18 @@
require "delegate"
module Timerage
# A range of time. The exposes the Range like interface.
- class TimeInterval < DelegateClass(Range)
- def initialize(*args)
- rng = if rangeish?(args.first)
- args.first
- else
- Range.new(*args)
- end
+ class TimeInterval < Range
- super rng
+ class << self
+ def new(*args)
+ args = [args.first.begin, args.first.end, args.first.exclude_end?] if args.first.respond_to?(:exclude_end?)
+ new_obj = allocate
+ new_obj.send(:initialize, *args)
+ new_obj
+ end
end
def to_time_interval
self
end
@@ -79,15 +79,15 @@
other_end < self_end
end
end
def overlap?(other)
- earliest, latest = if self.begin <= other.begin
- [self, other]
- else
- [other, self]
- end
+ if self.begin <= other.begin
+ earliest, latest = self, other
+ else
+ earliest, latest = other, self
+ end
latest_begin, earliest_end = latest.begin, earliest.end
return true if latest_begin < earliest_end
return false if earliest_end < latest_begin
@@ -132,29 +132,17 @@
def rangeish?(an_obj)
an_obj.respond_to?(:begin) &&
an_obj.respond_to?(:end)
end
- # ---
- #
- # This is implemented in a slightly more procedural style than i
- # prefer because we want to work well with ActiveSupport::Duration
- # steps. Adding a Duration to a time uses the timezone (dst, etc),
- # leap second and leap day aware `#advance` method in
- # ActiveSupport. However, multiplying a Duration by a number
- # returns a number, rather than a duration. This, in turn, means
- # that adding a duration times a number to a time results in
- # Timely incorrect results. So we do it the hard way.
def time_enumerator(step)
count = (self.end - self.begin).div(step) + 1
count -= 1 if exclude_end? and (self.end - self.begin) % step == 0
# We've included our end if it should be
Enumerator.new do |y|
- y << last = self.begin
-
- (count-1).times do
- y << last = last + step
+ (count).times do |offset|
+ y << self.begin + (step * offset)
end
end
end
# class methods