Class: Exchange::Money
- Inherits:
-
Object
- Object
- Exchange::Money
- Includes:
- Comparable
- Defined in:
- lib/exchange/money.rb
Overview
Money Objects instantiated from the money class can be used for basic mathematical operations and currency conversions
Instance Attribute Summary collapse
-
#api ⇒ Exchange::ExternalAPI
readonly
The current api subclass.
-
#currency ⇒ Symbol, String
Currency the currency of the money object.
-
#from ⇒ Exchange::Money
readonly
The original money object this money object was converted from.
-
#time ⇒ Time
The time at which the conversion has taken place or should take place if the object is involved in operations.
-
#value ⇒ BigDecimal
Number The number the money object has been instantiated from.
Instance Method Summary collapse
-
#*(other) ⇒ Exchange::Money
Multiply a value with the currency.
-
#+(other) ⇒ Exchange::Money
Add value to the currency.
-
#-(other) ⇒ Exchange::Money
Subtract a value from the currency.
-
#/(other) ⇒ Exchange::Money
Divide the currency by a value.
-
#<=>(other) ⇒ Fixed
Sortcompare a currency with another currency.
-
#==(other) ⇒ Boolean
Compare a currency with another currency or another value.
-
#ceil ⇒ Exchange::Money
Ceil the currency.
-
#floor ⇒ Exchange::Money
Floor the currency.
-
#initialize(value, currency_arg = nil, opts = {}) {|_self| ... } ⇒ Money
constructor
Intialize the currency with a number and a currency.
-
#method_missing(method, *args, &block) ⇒ Object
Method missing is used to handle conversions from one money object to another.
-
#round ⇒ Exchange::Money
Round the currency.
-
#to(other, options = {}) ⇒ Exchange::Money
(also: #in)
Converts this instance of currency into another currency.
-
#to_s(format = :currency) ⇒ String
Converts the currency to a string in ISO 4217 standardized format, either with or without the currency.
Constructor Details
#initialize(value, currency_arg = nil, opts = {}) {|_self| ... } ⇒ Money
Intialize the currency with a number and a currency
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/exchange/money.rb', line 52 def initialize value, currency_arg=nil, opts={}, &block currency_arg = ISO.assert_currency!(currency_arg) if currency_arg @from = opts[:from] @api = Exchange.configuration.api.subclass yield(self) if block_given? self.time = Helper.assure_time(time || opts[:at], :default => :now) self.value = ISO.instantiate(value, currency || currency_arg) self.currency = currency || currency_arg end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Method missing is used to handle conversions from one money object to another. It only handles currencies which are available in the API class set in the configuration.
72 73 74 |
# File 'lib/exchange/money.rb', line 72 def method_missing method, *args, &block value.send method, *args, &block end |
Instance Attribute Details
#api ⇒ Exchange::ExternalAPI (readonly)
Returns The current api subclass.
35 36 37 |
# File 'lib/exchange/money.rb', line 35 def api @api end |
#currency ⇒ Symbol, String
Returns currency the currency of the money object.
22 23 24 |
# File 'lib/exchange/money.rb', line 22 def currency @currency end |
#from ⇒ Exchange::Money (readonly)
Returns The original money object this money object was converted from.
31 32 33 |
# File 'lib/exchange/money.rb', line 31 def from @from end |
#time ⇒ Time
Returns The time at which the conversion has taken place or should take place if the object is involved in operations.
26 27 28 |
# File 'lib/exchange/money.rb', line 26 def time @time end |
#value ⇒ BigDecimal
Returns number The number the money object has been instantiated from.
18 19 20 |
# File 'lib/exchange/money.rb', line 18 def value @value end |
Instance Method Details
#*(other) ⇒ Exchange::Money
Multiply a value with the currency
233 |
# File 'lib/exchange/money.rb', line 233 base_operation '*' |
#+(other) ⇒ Exchange::Money
Add value to the currency
201 |
# File 'lib/exchange/money.rb', line 201 base_operation '+' |
#-(other) ⇒ Exchange::Money
Subtract a value from the currency
217 |
# File 'lib/exchange/money.rb', line 217 base_operation '-' |
#/(other) ⇒ Exchange::Money
Divide the currency by a value
249 |
# File 'lib/exchange/money.rb', line 249 base_operation '/' |
#<=>(other) ⇒ Fixed
which historic conversion should be used when two are present?
Sortcompare a currency with another currency. If the other is not an instance of Exchange::Money, the value of the currency is compared. Different currencies will be converted to the comparing instances currency
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/exchange/money.rb', line 289 def <=> other if is_same_currency?(other) value <=> other.value elsif is_other_currency?(other) test_for_currency_mix_error(other) value <=> other.to(currency, :at => other.time).value else value <=> other end end |
#==(other) ⇒ Boolean
Compare a currency with another currency or another value. If the other is not an instance of Exchange::Money, the value of the currency is compared
264 265 266 267 268 269 270 271 272 273 |
# File 'lib/exchange/money.rb', line 264 def == other if is_same_currency?(other) other.round.value == self.round.value elsif is_other_currency?(other) test_for_currency_mix_error(other) other.to(currency, :at => other.time).round.value == self.round.value else value == other end end |
#ceil ⇒ Exchange::Money
Ceil the currency. Since this is a currency, it will ceil to the standard decimal value. If you want to ceil it to another precision, you have to specifically ask for it.
168 |
# File 'lib/exchange/money.rb', line 168 install_operation :ceil |
#floor ⇒ Exchange::Money
Floor the currency. Since this is a currency, it will ceil to the standard decimal value. If you want to ceil it to another precision, you have to specifically ask for it.
184 |
# File 'lib/exchange/money.rb', line 184 install_operation :floor |
#round ⇒ Exchange::Money
Round the currency. Since this is a currency, it will round to the standard decimal value. If you want to round it to another precision, you have to specifically ask for it.
152 |
# File 'lib/exchange/money.rb', line 152 install_operation :round |
#to(other, options = {}) ⇒ Exchange::Money Also known as: in
Converts this instance of currency into another currency
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/exchange/money.rb', line 86 def to other, ={} other = ISO.assert_currency!(other) if api_supports_currency?(currency) && api_supports_currency?(other) opts = { :at => time, :from => self }.merge() Money.new(api.new.convert(value, currency, other, opts), other, opts) elsif fallback! to other, else raise_no_rate_error(other) end rescue ExternalAPI::APIError if fallback! to other, else raise end end |
#to_s(format = :currency) ⇒ String
Converts the currency to a string in ISO 4217 standardized format, either with or without the currency. This leaves you with no worries how to display the currency.
319 320 321 |
# File 'lib/exchange/money.rb', line 319 def to_s format=:currency ISO.stringify(value, currency, :format => format) end |