Sha256: 69bd0b82b4b2df0754647d26891d354d8511a24c0e4865639dad58f24e13b592

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

module TwitterCldr
  module Formatters
    class CurrencyFormatter < NumberFormatter

      def format(tokens, number, options = {})
        options[:currency] ||= "USD"
        currency = TwitterCldr::Shared::Currencies.for_code(options[:currency])
        currency ||= {
          :currency    => options[:currency],
          :symbol      => options[:currency],
          :cldr_symbol => options[:currency]
        }

        # overwrite with explicit symbol if given
        currency[:symbol] = options[:symbol] if options[:symbol]

        digits_and_rounding = resource(options[:currency])
        options[:precision] ||= digits_and_rounding[:digits]
        options[:rounding] ||= digits_and_rounding[:rounding]

        symbol = options[:use_cldr_symbol] ? currency[:cldr_symbol] : currency[:symbol]
        symbol ||= currency[:currency].to_s
        super.gsub('ยค', symbol)
      end

      private

      def resource(code)
        @resource ||= TwitterCldr.get_resource(:shared, :currency_digits_and_rounding)
        @resource[code.to_sym] || @resource[:DEFAULT]
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/formatters/numbers/currency_formatter.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/formatters/numbers/currency_formatter.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.2.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.2.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.1.2 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.1.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.1.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.10 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.9 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.8 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.7 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.6 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.5 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.4 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.3 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.2 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.1 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
twitter_cldr-3.0.0 lib/twitter_cldr/formatters/numbers/currency_formatter.rb