lib/exchange/money.rb in exchange-0.11.0 vs lib/exchange/money.rb in exchange-0.12.0
- old
+ new
@@ -46,18 +46,20 @@
# #=> #<Exchange::Money @number=40.0 @currency=:usd @time=#<Time>>
# @example Instantiate a money object of 40 US Dollars and convert it to Euro. It shows the conversion date and the original currency
# Exchange::Money.new(40, :usd).to(:eur, :at => Time.gm(2012,9,1))
# #=> #<Exchange::Money @number=37.0 @currency=:usd @time=#<Time> @from=#<Exchange::Money @number=40.0 @currency=:usd>>
#
- def initialize value, currency_arg=nil, opts={}, &block
+ 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 = ISO4217.instantiate(value, currency || currency_arg)
+ self.value = ISO.instantiate(value, currency || currency_arg)
self.currency = currency || currency_arg
end
# 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.
@@ -79,28 +81,31 @@
# Exchange::Money.new(40,:usd).to(:chf)
# @example convert to 'sek' at a specific rate
# Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
#
def to other, options={}
+ other = ISO.assert_currency!(other)
+
if api_supports_currency?(other)
opts = { :at => time, :from => self }.merge(options)
Money.new(api.new.convert(value, currency, other, opts), other, opts)
else
raise_no_rate_error(other)
end
end
+ alias :in :to
class << self
private
# @private
# @!macro [attach] install_operation
#
def install_operation op
define_method op do |*precision|
- Exchange::Money.new(ISO4217.send(op, self.value, self.currency, precision.first), currency, :at => time, :from => self)
+ Exchange::Money.new(ISO.send(op, self.value, self.currency, precision.first), currency, :at => time, :from => self)
end
end
# @private
# @!macro [attach] base_operation
@@ -108,11 +113,11 @@
#
def base_operation op
self.class_eval <<-EOV
def #{op}(other)
test_for_currency_mix_error(other)
- new_value = value #{op} (other.kind_of?(Money) ? other.to(self.currency, :at => other.time) : BigDecimal.new(other.to_s))
+ new_value = value #{op} (other.kind_of?(Money) ? other.to(self.currency, :at => other.time).value : BigDecimal.new(other.to_s))
Exchange::Money.new(new_value, currency, :at => time, :from => self)
end
EOV
end
@@ -290,15 +295,15 @@
# @example Convert a currency without minor to a string
# Exchange::Money.new(45, :jpy).to_s #=> "JPY 45"
# @example Convert a currency with a three decimal minor to a string
# Exchange::Money.new(34.34, :omr).to_s #=> "OMR 34.340"
# @example Convert a currency to a string without the currency
- # Exchange::ISO4217.stringif(34.34, :omr).to_s(:iso) #=> "34.340"
+ # Exchange::ISO.stringif(34.34, :omr).to_s(:iso) #=> "34.340"
#
def to_s format=:currency
[
- format == :currency && ISO4217.stringify(value, currency),
- format == :amount && ISO4217.stringify(value, currency, :amount_only => true)
+ format == :currency && ISO.stringify(value, currency),
+ format == :amount && ISO.stringify(value, currency, :amount_only => true)
].detect{|l| l.is_a?(String) }
end
private
\ No newline at end of file