lib/exchange/money.rb in exchange-0.10.2 vs lib/exchange/money.rb in exchange-0.11.0

- old
+ new

@@ -167,15 +167,15 @@ # Add value to the currency # @param [Integer, Float, Exchange::Money] other The value to be added to the currency. If an Exchange::Money, it is converted to the instance's currency and then the converted value is added. # @return [Exchange::Money] The currency with the added value - # @raise [CurrencyMixError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation + # @raise [ImplicitConversionError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation # @example Configuration disallows mixed operations - # Exchange.configuration.allow_mixed_operations = false + # Exchange.configuration.implicit_conversions = false # Exchange::Money.new(20,:nok) + Exchange::Money.new(20,:sek) - # #=> #<CurrencyMixError "You tried to mix currencies"> + # #=> #<ImplicitConversionError "You tried to mix currencies"> # @example Configuration allows mixed operations (default) # Exchange::Money.new(20,:nok) + Exchange::Money.new(20,:sek) # #=> #<Exchange::Money @value=37.56 @currency=:nok> # @since 0.1 # @version 0.7 @@ -183,15 +183,15 @@ base_operation '+' # Subtract a value from the currency # @param [Integer, Float, Exchange::Money] other The value to be subtracted from the currency. If an Exchange::Money, it is converted to the instance's currency and then subtracted from the converted value. # @return [Exchange::Money] The currency with the added value - # @raise [CurrencyMixError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation - # @example Configuration disallows mixed operations - # Exchange.configuration.allow_mixed_operations = false + # @raise [ImplicitConversionError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation + # @example Configuration disallows implicit conversions + # Exchange.configuration.implicit_conversions = false # Exchange::Money.new(20,:nok) - Exchange::Money.new(20,:sek) - # #=> #<CurrencyMixError "You tried to mix currencies"> + # #=> #<ImplicitConversionError "You tried to mix currencies"> # @example Configuration allows mixed operations (default) # Exchange::Money.new(20,:nok) - Exchange::Money.new(20,:sek) # #=> #<Exchange::Money @value=7.56 @currency=:nok> # @since 0.1 # @version 0.7 @@ -199,15 +199,15 @@ base_operation '-' # Multiply a value with the currency # @param [Integer, Float, Exchange::Money] other The value to be multiplied with the currency. If an Exchange::Money, it is converted to the instance's currency and multiplied with the converted value. # @return [Exchange::Money] The currency with the multiplied value - # @raise [CurrencyMixError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation + # @raise [ImplicitConversionError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation # @example Configuration disallows mixed operations - # Exchange.configuration.allow_mixed_operations = false + # Exchange.configuration.implicit_conversions = false # Exchange::Money.new(20,:nok) * Exchange::Money.new(20,:sek) - # #=> #<CurrencyMixError "You tried to mix currencies"> + # #=> #<ImplicitConversionError "You tried to mix currencies"> # @example Configuration allows mixed operations (default) # Exchange::Money.new(20,:nok) * Exchange::Money.new(20,:sek) # #=> #<Exchange::Money @value=70.56 @currency=:nok> # @since 0.1 # @version 0.7 @@ -215,15 +215,15 @@ base_operation '*' # Divide the currency by a value # @param [Integer, Float, Exchange::Money] other The value to be divided by the currency. If an Exchange::Money, it is converted to the instance's currency and divided by the converted value. # @return [Exchange::Money] The currency with the divided value - # @raise [CurrencyMixError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation + # @raise [ImplicitConversionError] If the configuration does not allow mixed operations, this method will raise an error if two different currencies are used in the operation # @example Configuration disallows mixed operations - # Exchange.configuration.allow_mixed_operations = false + # Exchange.configuration.implicit_conversions = false # Exchange::Money.new(20,:nok) / Exchange::Money.new(20,:sek) - # #=> #<CurrencyMixError "You tried to mix currencies"> + # #=> #<ImplicitConversionError "You tried to mix currencies"> # @example Configuration allows mixed operations (default) # Exchange::Money.new(20,:nok) / Exchange::Money.new(20,:sek) # #=> #<Exchange::Money @value=1.56 @currency=:nok> # @since 0.1 # @version 0.7 @@ -239,16 +239,17 @@ # @example Compare two different currencies, the other will get converted for comparison # Exchange::Money.new(40, :usd) == Exchange::Money.new(34, :eur) #=> true, will implicitly convert eur to usd at the actual rate # @example Compare a currency with a number, the value of the currency will get compared # Exchange::Money.new(35, :usd) == 35 #=> true # @since 0.1 - # @version 0.6 + # @version 0.11 # def == other if is_same_currency?(other) other.round.value == self.round.value - elsif is_currency?(other) + 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 @@ -256,11 +257,11 @@ # 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 # @param [Whatever you want to throw at it] other The counterpart to compare # @return [Fixed] a number which can be used for sorting # @since 0.3 - # @version 0.6 + # @version 0.11 # @todo which historic conversion should be used when two are present? # @example Compare two currencies in terms of value # Exchange::Money.new(40, :usd) <=> Exchange::Money.new(28, :usd) #=> -1 # @example Compare two different currencies, the other will get converted for comparison # Exchange::Money.new(40, :usd) <=> Exchange::Money.new(28, :eur) #=> -1 @@ -269,10 +270,11 @@ # 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 @@ -339,16 +341,16 @@ api::CURRENCIES.include?(currency) end # Test if another currency is used in an operation, and if so, if the operation is allowed # @param [Numeric, Exchange::Money] other The counterpart in the operation - # @raise [CurrencyMixError] an error if mixing currencies is not allowed and currencies where mixed + # @raise [ImplicitConversionError] 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.is_a?(Money) && other.currency != currency + raise ImplicitConversionError.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.implicit_conversions && 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 @@ -359,10 +361,10 @@ raise NoRateError.new("Cannot convert to #{other} because the defined api does not provide a rate") end end - # The error that will get thrown when currencies get mixed up in base operations + # The error that will get thrown when implicit conversions take place and are not allowed # - CurrencyMixError = Class.new(ArgumentError) + ImplicitConversionError = Class.new(StandardError) end \ No newline at end of file