lib/exchange/money.rb in exchange-0.9.0 vs lib/exchange/money.rb in exchange-0.10.0
- old
+ new
@@ -37,17 +37,17 @@
# @param [Integer, Float] value The number the currency is instantiated from
# @param [Symbol] currency_arg The currency the money object is in as a downcased symbol
# @param [Hash] opts Optional Parameters for instantiation
# @option opts [Time] :at The time at which conversion took place
# @option opts [String,Symbol] :from The money object this money object was converted from
- # @version 0.2
+ # @version 0.9
#
# @example Instantiate a money object of 40 US Dollars
# Exchange::Money.new(40, :usd)
# #=> #<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.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
@from = opts[:from]
@api = Exchange.configuration.api.subclass
@@ -59,66 +59,60 @@
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.
- # @example Calls convert_to with 'chf'
- # Exchange::Money.new(40,:usd).to_chf
- # @example Calls convert_to with 'sek' and :at => Time.gm(2012,2,2)
- # Exchange::Money.new(40,:nok).to_sek(:at => Time.gm(2012,2,2))
+ # @example convert to chf
+ # Exchange::Money.new(40,:usd).to(:chf)
+ # @example convert to sek at a given time
+ # Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
#
def method_missing method, *args, &block
value.send method, *args, &block
end
- ISO4217.currencies.each do |c|
- define_method :"to_#{c}" do |*args|
- if api_supports_currency?(c)
- convert_to c, { :at => time }.merge(args.first || {})
- else
- raise_no_rate_error(c)
- end
- end
- end
-
# Converts this instance of currency into another currency
# @return [Exchange::Money] An instance of Exchange::Money with the converted number and the converted currency
# @param [Symbol, String] other The currency to convert the number to
- # @param [Hash] opts An options hash
+ # @param [Hash] options An options hash
# @option [Time] :at The timestamp of the rate the conversion took place in
# @example convert to 'chf'
- # Exchange::Money.new(40,:usd).convert_to('chf')
+ # Exchange::Money.new(40,:usd).to(:chf)
# @example convert to 'sek' at a specific rate
- # Exchange::Money.new(40,:nok).convert_to('sek', :at => Time.gm(2012,2,2))
+ # Exchange::Money.new(40,:nok).to(:sek, :at => Time.gm(2012,2,2))
#
- def convert_to other, opts={}
- opts[:from] = self
- Money.new(api.new.convert(value, currency, other, opts), other, opts)
+ def to other, options={}
+ 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
class << self
private
# @private
- # @macro [attach] install_operations
+ # @!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)
end
end
# @private
- # @macro [attach] base_operations
+ # @!macro [attach] base_operation
# @method $1(other)
#
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.convert_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) : BigDecimal.new(other.to_s))
Exchange::Money.new(new_value, currency, :at => time, :from => self)
end
EOV
end
@@ -251,11 +245,11 @@
#
def == other
if is_same_currency?(other)
other.round.value == self.round.value
elsif is_currency?(other)
- other.convert_to(currency, :at => other.time).round.value == self.round.value
+ other.to(currency, :at => other.time).round.value == self.round.value
else
value == other
end
end
@@ -275,11 +269,11 @@
#
def <=> other
if is_same_currency?(other)
value <=> other.value
elsif is_other_currency?(other)
- value <=> other.convert_to(currency, :at => other.time).value
+ value <=> other.to(currency, :at => other.time).value
else
value <=> other
end
end
@@ -350,10 +344,10 @@
# @raise [CurrencyMixError] an error if mixing currencies is not allowed and currencies where mixed
# @since 0.6
# @version 0.6
#
def test_for_currency_mix_error other
- raise CurrencyMixError.new("You\'re trying to mix up #{currency} with #{other.currency}. You denied mixing currencies in the configuration, allow it or convert the currencies before mixing") if !Exchange.configuration.allow_mixed_operations && other.kind_of?(Money) && other.currency != currency
+ raise CurrencyMixError.new("You\'re trying to mix up #{currency} with #{other.currency}. You denied mixing currencies in the configuration, allow it or convert the currencies before mixing") if !Exchange.configuration.allow_mixed_operations && other.is_a?(Money) && other.currency != currency
end
# Helper method to raise a no rate error for a given currency if no rate is given
# @param [String] other a possible currency
# @raise [NoRateError] an error indicating that the given string is a currency, but no rate is present
\ No newline at end of file