Class: StrictMoney::Amount
- Inherits:
-
Object
- Object
- StrictMoney::Amount
- Extended by:
- Forwardable
- Includes:
- Comparable, Orms::Mongoid::Amount
- Defined in:
- lib/strict_money/amount.rb
Overview
A value object to describe an single amount of money with a single currency.
Constant Summary collapse
- IncompleteError =
Exception raised when somebody tries to instantiate an Amount with incomplete information.
Class.new(StandardError)
Instance Attribute Summary collapse
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
Instance Method Summary collapse
- #*(multiplier) ⇒ Object
- #+(addend) ⇒ Object
- #-(subtrahend) ⇒ Object
- #-@ ⇒ Object
- #/(divisor) ⇒ Object
- #<=>(other) ⇒ Object
- #as_float(currency) ⇒ Object
-
#initialize(amount, currency) ⇒ Amount
constructor
A new instance of Amount.
- #negative? ⇒ Boolean
- #positive? ⇒ Boolean
- #round(precision) ⇒ Object
Methods included from Orms::Mongoid::Amount
Constructor Details
#initialize(amount, currency) ⇒ Amount
Returns a new instance of Amount.
12 13 14 15 16 17 |
# File 'lib/strict_money/amount.rb', line 12 def initialize(amount, currency) raise IncompleteError unless amount && currency raise StrictMoney::UnsupportedCurrencyError unless StrictMoney.supported_currency?(currency) @amount = amount @currency = currency.upcase end |
Instance Attribute Details
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
10 11 12 |
# File 'lib/strict_money/amount.rb', line 10 def currency @currency end |
Instance Method Details
#*(multiplier) ⇒ Object
39 40 41 |
# File 'lib/strict_money/amount.rb', line 39 def *(multiplier) Amount.new(@amount * multiplier, currency) end |
#+(addend) ⇒ Object
29 30 31 32 |
# File 'lib/strict_money/amount.rb', line 29 def +(addend) raise TypeError unless addend.is_a?(Amount) Amount.new(as_float(currency) + addend.as_float(currency), currency) end |
#-(subtrahend) ⇒ Object
34 35 36 37 |
# File 'lib/strict_money/amount.rb', line 34 def -(subtrahend) raise TypeError unless subtrahend.is_a?(Amount) self + (subtrahend * -1) end |
#-@ ⇒ Object
19 20 21 |
# File 'lib/strict_money/amount.rb', line 19 def -@ Amount.new(-@amount, @currency) end |
#/(divisor) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/strict_money/amount.rb', line 43 def /(divisor) if divisor.is_a?(Amount) @amount / divisor.as_float(@currency) else Amount.new(@amount / divisor, @currency) end end |
#<=>(other) ⇒ Object
23 24 25 26 27 |
# File 'lib/strict_money/amount.rb', line 23 def <=>(other) return unless other.is_a?(Amount) && currency == other.currency as_float(currency) <=> other.as_float(currency) end |
#as_float(currency) ⇒ Object
51 52 53 54 |
# File 'lib/strict_money/amount.rb', line 51 def as_float(currency) raise WrongCurrencyError unless currency.upcase == @currency @amount.to_f end |
#negative? ⇒ Boolean
56 57 58 |
# File 'lib/strict_money/amount.rb', line 56 def negative? as_float(@currency) < 0 end |
#positive? ⇒ Boolean
60 61 62 |
# File 'lib/strict_money/amount.rb', line 60 def positive? as_float(@currency) > 0 end |