Sha256: 11ad1e9798fb9d6af3077897511e723ab220c6f3a92879fb7b80065d7727acba

Contents?: true

Size: 743 Bytes

Versions: 91

Compression:

Stored size: 743 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

91 entries across 87 versions & 13 rubygems

Version Path
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/number_helper/number_to_delimited_converter.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.6/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.6 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.5.1 lib/active_support/number_helper/number_to_delimited_converter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/number_helper/number_to_delimited_converter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/number_helper/number_to_delimited_converter.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/number_helper/number_to_delimited_converter.rb
fablicop-1.10.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.5/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.5 lib/active_support/number_helper/number_to_delimited_converter.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/activesupport-7.0.4.3/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.4.3 lib/active_support/number_helper/number_to_delimited_converter.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.4.2 lib/active_support/number_helper/number_to_delimited_converter.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.4.1 lib/active_support/number_helper/number_to_delimited_converter.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.4 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.3.1 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.3 lib/active_support/number_helper/number_to_delimited_converter.rb
activesupport-7.0.2.4 lib/active_support/number_helper/number_to_delimited_converter.rb