Class: Mint::Money::CoercedNumber
- Inherits:
-
Object
- Object
- Mint::Money::CoercedNumber
- Includes:
- Comparable
- Defined in:
- lib/minting/money/coercion.rb
Overview
Coerced Number contains the arithmetic logic for numeric compatible ops.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
-
#initialize(value) ⇒ CoercedNumber
constructor
A new instance of CoercedNumber.
- #raise_coercion_error(operation, operand) ⇒ Object
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
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 |