Module: Timespan::Compare
- Includes:
- Comparable
- Defined in:
- lib/timespan/compare.rb
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(time) ⇒ Object
- #expired? ⇒ Boolean
- #time_left(time = nil) ⇒ Object
- #valid_compare?(time) ⇒ Boolean
- #valid_compare_types ⇒ Object
Instance Method Details
#+(other) ⇒ Object
50 51 52 53 |
# File 'lib/timespan/compare.rb', line 50 def +(other) self.duration += Duration.new(other) self end |
#-(other) ⇒ Object
55 56 57 58 |
# File 'lib/timespan/compare.rb', line 55 def -(other) self.duration -= Duration.new(other) self end |
#<=>(time) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/timespan/compare.rb', line 34 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 |
#expired? ⇒ Boolean
30 31 32 |
# File 'lib/timespan/compare.rb', line 30 def expired? time_left.total <= 0 end |
#time_left(time = nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/timespan/compare.rb', line 24 def time_left time = nil time_compare = time || Time.now diff = end_time - time_compare Timespan::TimeDuration.new(diff) end |
#valid_compare?(time) ⇒ Boolean
61 62 63 |
# File 'lib/timespan/compare.rb', line 61 def valid_compare? time valid_compare_types.any? {|type| time.kind_of? type } end |
#valid_compare_types ⇒ Object
65 66 67 |
# File 'lib/timespan/compare.rb', line 65 def valid_compare_types [Timespan, Time, Date, DateTime, Integer] end |