class ActiveSupport::Duration::Scalar

:nodoc:

def %(other)

def %(other)
  if Duration === other
    Duration.build(value % other.value)
  else
    calculate(:%, other)
  end
end

def *(other)

def *(other)
  if Duration === other
    new_parts = other.parts.map { |part, other_value| [part, value * other_value] }.to_h
    new_value = value * other.value
    Duration.new(new_value, new_parts)
  else
    calculate(:*, other)
  end
end

def +(other)

def +(other)
  if Duration === other
    seconds   = value + other.parts[:seconds]
    new_parts = other.parts.merge(seconds: seconds)
    new_value = value + other.value
    Duration.new(new_value, new_parts)
  else
    calculate(:+, other)
  end
end

def -(other)

def -(other)
  if Duration === other
    seconds   = value - other.parts[:seconds]
    new_parts = other.parts.map { |part, other_value| [part, -other_value] }.to_h
    new_parts = new_parts.merge(seconds: seconds)
    new_value = value - other.value
    Duration.new(new_value, new_parts)
  else
    calculate(:-, other)
  end
end

def -@

def -@
  Scalar.new(-value)
end

def /(other)

def /(other)
  if Duration === other
    value / other.value
  else
    calculate(:/, other)
  end
end

def <=>(other)

def <=>(other)
  if Scalar === other || Duration === other
    value <=> other.value
  elsif Numeric === other
    value <=> other
  else
    nil
  end
end

def calculate(op, other)

def calculate(op, other)
  if Scalar === other
    Scalar.new(value.public_send(op, other.value))
  elsif Numeric === other
    Scalar.new(value.public_send(op, other))
  else
    raise_type_error(other)
  end
end

def coerce(other)

def coerce(other)
  [Scalar.new(other), self]
end

def initialize(value)

def initialize(value)
  @value = value
end

def raise_type_error(other)

def raise_type_error(other)
  raise TypeError, "no implicit conversion of #{other.class} into #{self.class}"
end