Sha256: 8790703074d692df78597935c9ceb9e42e1b643b7486aa6795ed0eb0b5c36147

Contents?: true

Size: 1.29 KB

Versions: 48

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

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

48 entries across 48 versions & 7 rubygems

Version Path
activesupport-5.2.8.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.8 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.7.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.6.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.6.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.6.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.5 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.5 lib/active_support/number_helper/number_to_currency_converter.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-5.2.4.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.4 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.2 lib/active_support/number_helper/number_to_currency_converter.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/activesupport-5.2.4.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.1 lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-5.2.4.rc1 lib/active_support/number_helper/number_to_currency_converter.rb