lib/timespan/compare.rb in timespan-0.3.1 vs lib/timespan/compare.rb in timespan-0.3.2
- old
+ new
@@ -19,10 +19,24 @@
end
module Compare
include Comparable
+ 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
+
def time_left time = nil
time_compare = time || now
diff = end_time - time_compare
Timespan::TimeDuration.new(diff)
end
@@ -53,9 +67,15 @@
end
def -(other)
self.duration -= Duration.new(other)
self
+ end
+
+ protected
+
+ def any_kind_of? obj, *types
+ types.flatten.compact.any?{|type| obj.kind_of? type }
end
def valid_compare? time
valid_compare_types.any? {|type| time.kind_of? type }
\ No newline at end of file