Sha256: 4c8b402b809b88752554fc2efd4f0fb4108e3593d619cf602c7ef3d9bf68a678

Contents?: true

Size: 742 Bytes

Versions: 68

Compression:

Stored size: 742 Bytes

Contents

# frozen_string_literal: true

require "active_support/number_helper/number_converter"

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

68 entries across 68 versions & 9 rubygems

Version Path
activesupport-6.1.7.10 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.9 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.8 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.7 lib/active_support/number_helper/number_to_delimited_converter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.6 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.5 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.4 lib/active_support/number_helper/number_to_delimited_converter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/number_helper/number_to_delimited_converter.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/number_to_delimited_converter.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.6.1/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.3 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.2 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.0.6.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.7 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.0.6 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.1.6.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.0.5.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-6.0.5 lib/active_support/number_helper/number_to_delimited_converter.rb