Module: Timespan::Compare
- Includes:
- Comparable
- Defined in:
- lib/timespan/compare.rb,
lib/timespan/extensions/time_lord.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(time) ⇒ Object
- #between?(cfrom, cto) ⇒ Boolean
- #expired? ⇒ Boolean
- #time_left(time = nil) ⇒ Object
Instance Method Details
#+(other) ⇒ Object
64 65 66 67 |
# File 'lib/timespan/compare.rb', line 64 def +(other) self.duration += Duration.new(other) self end |
#-(other) ⇒ Object
69 70 71 72 |
# File 'lib/timespan/compare.rb', line 69 def -(other) self.duration -= Duration.new(other) self end |
#<=>(time) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 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? ⇒ Boolean
44 45 46 |
# File 'lib/timespan/compare.rb', line 44 def expired? time_left.total <= 0 end |
#time_left(time = nil) ⇒ Object
38 39 40 41 42 |
# 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 |