Module: ActivePeriod::Comparable
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#clamp_on(other) ⇒ Object
Reverse clamp where the ‘other` is clamped by `self` If other is an ActivePeriod::Period then `clamp_on` will return an ActivePeriod::Period If other is not an ActivePeriod::Period then `clamp_on` will return a range.
- #include?(other) ⇒ Boolean
Instance Method Details
#<=>(other) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/active_period/comparable.rb', line 20 def <=>(other) case other when DateTime, Time, ActiveSupport::TimeWithZone if first < other && last < other -1 elsif first > other && last > other 1 else 0 end when ActiveSupport::Duration, Numeric to_i <=> other.to_i else if self.class != other.class raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable]) else (self.begin <=> other) end end end |
#clamp_on(other) ⇒ Object
Reverse clamp where the ‘other` is clamped by `self`
If other is an ActivePeriod::Period then `clamp_on` will return an ActivePeriod::Period
If other is not an ActivePeriod::Period then `clamp_on` will return a range
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_period/comparable.rb', line 44 def clamp_on(other) case other when DateTime, Time, ActiveSupport::TimeWithZone case self <=> other when -1 last when 0 other when 1 first end when ActivePeriod::Period self & other when Range if (other.first > last && other.last > last) || (other.first < first && other.last < first) return nil end clamped = clamp_on(other.first) .. clamp_on(other.last) if other.is_a? ActivePeriod::Period clamped.to_period else clamped end else raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable]) end # # new_first = other.first < first ? first : other.first # new_last = other.last > last ? last : other.last # # if other.is_a? ActivePeriod::Period # Period.new(new_first .. new_last) # else # new_first .. new_last # end end |
#include?(other) ⇒ Boolean
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_period/comparable.rb', line 5 def include?(other) case other when DateTime, Time, ActiveSupport::TimeWithZone include_time?(other) when Date include_period?(ActivePeriod::Day.new(other)) when ActivePeriod::Period include_period?(other) when Range include?(other.first) && include?(other.last) else raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable]) end end |