module Timespan::Compare

Public Instance Methods

+(other) click to toggle source
# File lib/timespan/compare.rb, line 64
def +(other)
  self.duration += Duration.new(other)
  self
end
-(other) click to toggle source
# File lib/timespan/compare.rb, line 69
def -(other)
  self.duration -= Duration.new(other)
  self
end
<=>(time) click to toggle source
# File lib/timespan/compare.rb, line 48
def <=> time
    raise ArgumentError, "Not a valid argument for Timespan comparison, was #{time}" unless valid_compare?(time)
    case time
    when Timespan
            seconds <=> time.seconds
    when Time                  
    seconds <=> time.to_i
  when Date, DateTime
    time.to_time.to_i
  when Integer
    seconds <=> time
  when ActiveSupport::Duration
    seconds <=> time.to_i
  end
end
between?(cfrom, cto) click to toggle source
# File lib/timespan/compare.rb, line 24
def between? cfrom, cto
  case cfrom
  when Date, Time, DateTime
    unless any_kind_of?(cto, Time, Date, DateTime)
      raise ArgumentError, "Arguments must both be Date or Time, was: #{cfrom}, #{cto}"
    end
    (self.start_time.to_i >= cfrom.to_i) && (self.end_time.to_i <= cto.to_i)
  when ::Duration, String, Integer, ActiveSupport::Duration
    self >= cfrom && self <= cto
  else
    raise ArgumentError, "Not valid arguments for between comparison: #{cfrom}, #{cto}"
  end
end
expired?() click to toggle source
# File lib/timespan/compare.rb, line 44
def expired?
  time_left.total <= 0
end
time_left(time = nil) click to toggle source
# File lib/timespan/compare.rb, line 38
def time_left time = nil
  time_compare = time || now
  diff = end_time - time_compare
  Timespan::TimeDuration.new(diff)
end

Protected Instance Methods

any_kind_of?(obj, *types) click to toggle source
# File lib/timespan/compare.rb, line 76
def any_kind_of? obj, *types
  types.flatten.compact.any?{|type| obj.kind_of? type }
end
valid_compare?(time) click to toggle source
# File lib/timespan/compare.rb, line 81
def valid_compare? time
  valid_compare_types.any? {|type| time.kind_of? type }
end
valid_compare_types() click to toggle source
# File lib/timespan/compare.rb, line 85
def valid_compare_types
  [Timespan, Time, Date, DateTime, Integer]
end