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.



16
17
18
# File 'lib/minting/money/coercion.rb', line 16

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



32
33
34
# File 'lib/minting/money/coercion.rb', line 32

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

#+(other) ⇒ Object



20
21
22
23
24
# File 'lib/minting/money/coercion.rb', line 20

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

  raise_coercion_error(:+, other)
end

#-(other) ⇒ Object



26
27
28
29
30
# File 'lib/minting/money/coercion.rb', line 26

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

  raise_coercion_error(:-, other)
end

#/(other) ⇒ Object



36
37
38
# File 'lib/minting/money/coercion.rb', line 36

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

#<=>(other) ⇒ Object



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

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)


47
48
49
50
# File 'lib/minting/money/coercion.rb', line 47

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