Sha256: b74a7033529dd40a57bf449f10bf45482dfd9e57f1efb510bdec05846e42a609

Contents?: true

Size: 1.25 KB

Versions: 50

Compression:

Stored size: 1.25 KB

Contents

require 'active_support/core_ext/numeric/inquiry'

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

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

        if number.to_f.negative?
          format = options[:negative_format]
          number = absolute_value(number)
        end

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

      private

        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 are given
            defaults[:negative_format] = "-#{opts[:format]}" if opts[:format]
            defaults.merge!(opts)
          end
        end

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

Version data entries

50 entries across 48 versions & 8 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/activesupport-5.0.7.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.7.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.7.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.6.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.5 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.5.rc2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.5.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.4 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.4.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.0.3 lib/active_support/number_helper/number_to_currency_converter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
lazy_record-0.2.1 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
lazy_record-0.2.0 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
lazy_record-0.1.9 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
lazy_record-0.1.8 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb
lazy_record-0.1.7 vendor/bundle/gems/activesupport-5.0.2/lib/active_support/number_helper/number_to_currency_converter.rb