123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::NumberHelper::NumberToDelimitedConverter

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveSupport::NumberHelper::NumberConverter
Defined in: activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb

Constant Summary

NumberConverter - Inherited

DEFAULTS, EMPTY_FORMAT

Class Attribute Summary

Class Method Summary

NumberConverter - Inherited

.convert,
.convert_with_resolved_options

Used by a converter delegating to another one: the options it passes already hold every key of the defaults and of the locale's number.format, so only this converter's namespace is resolved again.

.i18n_format_key, .new

Instance Attribute Summary

Instance Method Summary

NumberConverter - Inherited

#execute, #default_format_options, #default_value, #i18n_format_options, #namespace_format_options,
#options

The format options, in increasing order of precedence: the defaults, the namespace's defaults, the locale's number.format, the locale's number..format, and the options given to the call.

#translate_in_locale, #translate_number_value_with_default, #valid_bigdecimal

Constructor Details

This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter

Instance Method Details

#convert

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb', line 11

def convert
  number_string = number.to_s
  return number_string if number.respond_to?(:finite?) && !number.finite?

  if number_string.include?(".")
    left, right = number_string.split(".")
  else
    # Integers are by far the most common input, and their to_s is a
    # fresh String that can be delimited in place. A String number is
    # the caller's own object, so copy it, into a plain String as split
    # would have produced, whatever subclass it was.
    left = number.is_a?(::String) ? ::String.new(number_string) : number_string
  end

  if delimiter_pattern
    left.gsub!(delimiter_pattern) do |digit_to_delimit|
      "#{digit_to_delimit}#{options[:delimiter]}"
    end
  elsif (delimiter = options[:delimiter])
    # Insert the delimiter every three digits from the right, so that
    # earlier insertion points stay valid and no group strings are built.
    first_digit = left.start_with?("-", "+") ? 1 : 0
    position = left.size - 3
    while position > first_digit
      left.insert(position, delimiter)
      position -= 3
    end
  end

  right ? "#{left}#{options[:separator]}#{right}" : left
end

#delimiter_pattern (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_delimited_converter.rb', line 44

def delimiter_pattern
  options[:delimiter_pattern]
end