Sha256: 51fbfa707178b7feb523ee022378fe2a667ce274767e20acb872689955dde232

Contents?: true

Size: 1.26 KB

Versions: 53

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

53 entries across 51 versions & 10 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.11.2 lib/active_support/number_helper/number_to_currency_converter.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/number_helper/number_to_currency_converter.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.11.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.11 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.10 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.10.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.9 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.9.rc2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.9.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.8 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.8.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.7.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.7.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-4.2.6.rc1 lib/active_support/number_helper/number_to_currency_converter.rb