Sha256: 5c37be11c7ec19f1e75ad3a586b66ab6ed7f92082a98d38bfe124e26063f0d45

Contents?: true

Size: 656 Bytes

Versions: 4

Compression:

Stored size: 656 Bytes

Contents

module ActiveSupport
  module NumberHelper
    class NumberToDelimitedConverter < NumberConverter #:nodoc:
      self.validate_float = true

      DEFAULT_DELIMITER_REGEX = /(\d)(?=(\d\d\d)+(?!\d))/

      def convert
        parts.join(options[:separator])
      end

      private

        def parts
          left, right = number.to_s.split('.')
          left.gsub!(delimiter_pattern) do |digit_to_delimit|
            "#{digit_to_delimit}#{options[:delimiter]}"
          end
          [left, right].compact
        end

        def delimiter_pattern
          options.fetch(:delimiter_pattern, DEFAULT_DELIMITER_REGEX)
        end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activesupport-5.0.0.beta3 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-5.0.0.beta2 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-5.0.0.beta1.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-5.0.0.beta1 lib/active_support/number_helper/number_to_delimited_converter.rb