lib/exchange/money.rb in exchange-0.12.0 vs lib/exchange/money.rb in exchange-1.0.0

- old
+ new

@@ -1,5 +1,6 @@ +# -*- encoding : utf-8 -*- # Top Level Module of the the gem. # @author Beat Richartz # @version 0.9 # @since 0.1 @@ -86,13 +87,21 @@ 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) + elsif fallback! + to other, options else raise_no_rate_error(other) end + rescue ExternalAPI::APIError + if fallback! + to other, options + else + raise + end end alias :in :to class << self @@ -101,11 +110,12 @@ # @private # @!macro [attach] install_operation # def install_operation op define_method op do |*precision| - Exchange::Money.new(ISO.send(op, self.value, self.currency, precision.first), currency, :at => time, :from => self) + psych = precision.first == :psych + Exchange::Money.new(ISO.send(op, self.value, self.currency, psych ? nil : precision.first, {:psych => psych}), currency, :at => time, :from => self) end end # @private # @!macro [attach] base_operation @@ -294,22 +304,38 @@ # Exchange::Money.new(49.567, :usd).to_s #=> "USD 49.57" # @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::ISO.stringif(34.34, :omr).to_s(:iso) #=> "34.340" + # @example Convert a currency with a three decimal minor to a string with a currency symbol + # Exchange::Money.new(34.34, :usd).to_s(:symbol) #=> "$34.34" + # @example Convert a currency with a three decimal minor to a string with just the amount + # Exchange::Money.new(34.34, :omr).to_s(:amount) #=> "34.340" # def to_s format=:currency - [ - format == :currency && ISO.stringify(value, currency), - format == :amount && ISO.stringify(value, currency, :amount_only => true) - ].detect{|l| l.is_a?(String) } + ISO.stringify(value, currency, :format => format) end private + # Fallback to the next api defined in the api fallbacks. Changes the api for the given instance + # @return [Boolean] true if the fallback was successful, false if not + # @since 1.0 + # @version 1.0 + # + def fallback! + fallback = Exchange.configuration.api.fallback + new_api = fallback.index(api) ? fallback[fallback.index(api) + 1] : fallback.first + + if new_api + @api = new_api + return true + end + + return false + end + # determine if another given object is an instance of Exchange::Money # @param [Object] other The object to be tested against # @return [Boolean] true if the other is an instance of Exchange::Money, false if not # @since 0.6 # @version 0.6 @@ -361,15 +387,15 @@ # @raise [NoRateError] an error indicating that the given string is a currency, but no rate is present # @since 0.7.2 # @version 0.7.2 # def raise_no_rate_error other - raise NoRateError.new("Cannot convert to #{other} because the defined api does not provide a rate") + raise NoRateError.new("Cannot convert to #{other} because the defined api nor the fallbacks provide a rate") end end # The error that will get thrown when implicit conversions take place and are not allowed # ImplicitConversionError = Class.new(StandardError) -end \ No newline at end of file +end