Module: ActivePeriod::Comparable

Includes:
Comparable
Included in:
Period
Defined in:
lib/active_period/comparable.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_period/comparable.rb', line 18

def <=>(other)
  raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable]) unless other.is_a?(ActiveSupport::Duration)

  if other.is_a?(ActiveSupport::Duration) || other.is_a?(Numeric)
    to_i <=> other.to_i
  elsif self.class != other.class
    raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable])
  else
    (self.begin <=> other)
  end
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
# 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)
  else
    raise ArgumentError, I18n.t(:incomparable_error, scope: %i[active_period comparable])
  end
end