Class: Mint::Money::CoercedNumber

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/minting/money/coercion.rb

Overview

:nodoc Coerced Number contains the arithmetic logic for numeric compatible ops.

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ CoercedNumber

Returns a new instance of CoercedNumber.



14
15
16
# File 'lib/minting/money/coercion.rb', line 14

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



30
31
32
# File 'lib/minting/money/coercion.rb', line 30

def *(other)
  other.mint(@value * other.amount)
end

#+(other) ⇒ Object



18
19
20
21
22
# File 'lib/minting/money/coercion.rb', line 18

def +(other)
  return other if @value.zero?

  raise_coercion_error(:+, other)
end

#-(other) ⇒ Object



24
25
26
27
28
# File 'lib/minting/money/coercion.rb', line 24

def -(other)
  return -other if @value.zero?

  raise_coercion_error(:-, other)
end

#/(other) ⇒ Object



34
35
36
# File 'lib/minting/money/coercion.rb', line 34

def /(other)
  raise_coercion_error(:/, other)
end

#<=>(other) ⇒ Object



38
39
40
41
42
43
# File 'lib/minting/money/coercion.rb', line 38

def <=>(other)
  return nil if @value.nil? || other.nil?
  return @value <=> other.amount if @value.zero? || other.zero?

  raise_coercion_error(:<=>, other)
end

#raise_coercion_error(operation, operand) ⇒ Object

Raises:

  • (TypeError)


45
46
47
48
# File 'lib/minting/money/coercion.rb', line 45

def raise_coercion_error(operation, operand)
  raise TypeError,
        "#{self} #{operation} #{operand} : incompatible operands"
end