Sha256: 8164a1dd19d29c7a7748f778a330ea2a3162d3cd9b9ee546467492b631f999a2

Contents?: true

Size: 1.26 KB

Versions: 49

Compression:

Stored size: 1.26 KB

Contents

module ActiveSupport
  module NumberHelper
    class NumberToCurrencyConverter < NumberConverter # :nodoc:
      self.namespace = :currency

      def convert
        number = self.number.to_s.strip
        format = options[:format]

        if is_negative?(number)
          format = options[:negative_format]
          number = absolute_value(number)
        end

        rounded_number = NumberToRoundedConverter.convert(number, options)
        format.gsub('%n', rounded_number).gsub('%u', options[:unit])
      end

      private

        def is_negative?(number)
          number.to_f.phase != 0
        end

        def absolute_value(number)
          number.respond_to?("abs") ? number.abs : number.sub(/\A-/, '')
        end

        def options
          @options ||= begin
            defaults = default_format_options.merge(i18n_opts)
            # Override negative format if format options is given
            defaults[:negative_format] = "-#{opts[:format]}" if opts[:format]
            defaults.merge!(opts)
          end
        end

        def i18n_opts
          # Set International negative format if not exists
          i18n = i18n_format_options
          i18n[:negative_format] ||= "-#{i18n[:format]}" if i18n[:format]
          i18n
        end
    end
  end
end

Version data entries

49 entries across 49 versions & 4 rubygems

Version Path
activesupport-4.1.16 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.16.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.15 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.15.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.14.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.14.1 lib/active_support/number_helper/number_to_currency_converter.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/number_helper/number_to_currency_converter.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/number_helper/number_to_currency_converter.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.14 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.14.rc2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.14.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.13 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.13.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.12 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.12.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.11 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.10 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.10.rc4 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.1.10.rc3 lib/active_support/number_helper/number_to_currency_converter.rb