Class: Mint::Money::CoercedNumber

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

Overview

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.



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

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



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

def *(other)
  other.mint(@value * other.amount)
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



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

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

  raise_coercion_error(:-, other)
end

#/(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



48
49
50
51
52
53
# File 'lib/minting/money/coercion.rb', line 48

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)


56
57
58
59
# File 'lib/minting/money/coercion.rb', line 56

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