lib/exchange/iso.rb in exchange-1.0.4 vs lib/exchange/iso.rb in exchange-1.1.0

- old
+ new

@@ -21,11 +21,11 @@ # @private # @macro [attach] install_operations def install_operation op self.class_eval <<-EOV - def #{op}(amount, currency, precision=nil, opts={}) + def #{op} amount, currency, precision=nil, opts={} minor = definitions[currency][:minor_unit] money = amount.is_a?(BigDecimal) ? amount : BigDecimal.new(amount.to_s, precision_for(amount, currency)) if opts[:psych] && minor > 0 money.#{op}(0) - BigDecimal.new((1.0/(10**minor)).to_s) elsif opts[:psych] @@ -85,11 +85,11 @@ # @return [BigDecimal] The instantiated currency # @example instantiate a currency from a string # Exchange::ISO.instantiate("4523", "usd") #=> #<Bigdecimal 4523.00> # @note Reinstantiation is not needed in case the amount is already a big decimal. In this case, the maximum precision is already given. # - def instantiate(amount, currency) + def instantiate amount, currency if amount.is_a?(BigDecimal) amount else BigDecimal.new(amount.to_s, precision_for(amount, currency)) end @@ -109,29 +109,33 @@ # @example Convert a currency with a three decimal minor to a string # Exchange::ISO.stringif(34.34, :omr) #=> "OMR 34.340" # @example Convert a currency to a string without the currency # Exchange::ISO.stringif(34.34, :omr, :amount_only => true) #=> "34.340" # - def stringify(amount, currency, opts={}) + def stringify amount, currency, opts={} definition = definitions[currency] separators = definition[:separators] || {} format = "%.#{definition[:minor_unit]}f" string = format % amount major, minor = string.split('.') + + major.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) { $1 + separators[:major] } if separators[:major] && opts[:format] != :plain - if separators[:major] && opts[:format] != :plain - major.reverse! - major.gsub!(/(\d{3})(?=.)/) { $1 + separators[:major] } - major.reverse! - end - string = minor ? major + (opts[:format] == :plain || !separators[:minor] ? '.' : separators[:minor]) + minor : major pre = [[:amount, :plain].include?(opts[:format]) && '', opts[:format] == :symbol && definition[:symbol], currency.to_s.upcase + ' '].detect{|a| a.is_a?(String)} "#{pre}#{string}" end + # Returns the symbol for a given currency. Returns nil if no symbol is present + # @param currency The currency to return the symbol for + # @return [String, NilClass] The symbol or nil + # + def symbol currency + definitions[currency][:symbol] + end + # Use this to round a currency amount. This allows us to round exactly to the number of minors the currency has in the # iso definition # @param [BigDecimal, Fixed, Float, String] amount The amount of money you want to round # @param [String, Symbol] currency The currency you want to round the money in # @example Round a currency with 2 minors @@ -157,11 +161,11 @@ install_operation :floor # Forwards the assure_time method to the instance using singleforwardable # - def_delegators :instance, :definitions, :instantiate, :stringify, :round, :ceil, :floor, :currencies, :country_map, :defines?, :assert_currency! + def_delegators :instance, :definitions, :instantiate, :stringify, :symbol, :round, :ceil, :floor, :currencies, :country_map, :defines?, :assert_currency! private # symbolizes keys and returns a new hash # @@ -173,9 +177,12 @@ new_hsh[k.downcase.to_sym] = v end new_hsh end + + # Interpolates a string with separators every 3 characters + # # get a precision for a specified amount and a specified currency # @params [Float, Integer] amount The amount to get the precision for # @params [Symbol] currency the currency to get the precision for #