Sha256: dd1ee5f06a5639d24848ab7de70e0f833c1a134053645b7d1a6efef02c138a90

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require "active_support/number_helper/number_converter"

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

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

        if number_f.negative?
          number = number_f.abs

          unless options[:precision] == 0 && number < 0.5
            format = options[:negative_format]
          end
        end

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

      private
        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

10 entries across 10 versions & 2 rubygems

Version Path
activesupport-6.0.3.7 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.6 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.5 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.4 lib/active_support/number_helper/number_to_currency_converter.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.2/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.3.rc1 lib/active_support/number_helper/number_to_currency_converter.rb