Sha256: 1e2e98485decd3a163dba5604b5dc12a6155de75f807f7cfc682d6809157d130

Contents?: true

Size: 1.28 KB

Versions: 25

Compression:

Stored size: 1.28 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
        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", rounded_number).gsub("%u", 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

25 entries across 23 versions & 5 rubygems

Version Path
activesupport-6.0.2.2 lib/active_support/number_helper/number_to_currency_converter.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/number_helper/number_to_currency_converter.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.2.1/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.2.1 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.2 lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.2.rc2 lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.2.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.1 lib/active_support/number_helper/number_to_currency_converter.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.1.rc1 lib/active_support/number_helper/number_to_currency_converter.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/number_helper/number_to_currency_converter.rb
activesupport-6.0.0 lib/active_support/number_helper/number_to_currency_converter.rb