Sha256: 798c462ce9f56053c435d6a1b3ef59b32b6955cb8816eb3f7d0e4fd5624e09ef

Contents?: true

Size: 1.63 KB

Versions: 45

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/object/blank"
require "active_support/number_helper/number_converter"

module ActiveSupport
  module NumberHelper
    class NumberToPhoneConverter < NumberConverter # :nodoc:
      def convert
        str = country_code(opts[:country_code]).dup
        str << convert_to_phone_number(number.to_s.strip)
        str << phone_ext(opts[:extension])
      end

      private
        def convert_to_phone_number(number)
          if opts[:area_code]
            convert_with_area_code(number)
          else
            convert_without_area_code(number)
          end
        end

        def convert_with_area_code(number)
          default_pattern = /(\d{1,3})(\d{3})(\d{4}$)/
          number.gsub!(regexp_pattern(default_pattern),
                       "(\\1) \\2#{delimiter}\\3")
          number
        end

        def convert_without_area_code(number)
          default_pattern = /(\d{0,3})(\d{3})(\d{4})$/
          number.gsub!(regexp_pattern(default_pattern),
                       "\\1#{delimiter}\\2#{delimiter}\\3")
          number.slice!(0, 1) if start_with_delimiter?(number)
          number
        end

        def start_with_delimiter?(number)
          delimiter.present? && number.start_with?(delimiter)
        end

        def delimiter
          opts[:delimiter] || "-"
        end

        def country_code(code)
          code.blank? ? "" : "+#{code}#{delimiter}"
        end

        def phone_ext(ext)
          ext.blank? ? "" : " x #{ext}"
        end

        def regexp_pattern(default_pattern)
          opts.fetch :pattern, default_pattern
        end
    end
  end
end

Version data entries

45 entries across 45 versions & 8 rubygems

Version Path
activesupport-8.0.2 lib/active_support/number_helper/number_to_phone_converter.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/activesupport-8.0.1/lib/active_support/number_helper/number_to_phone_converter.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.0.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.2.2.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.1.5.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.0 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.2.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.1.5 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.0.rc2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.2.1.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.1.4.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.0.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.2.1.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-7.1.4.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-8.0.0.beta1 lib/active_support/number_helper/number_to_phone_converter.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/number_helper/number_to_phone_converter.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/number_helper/number_to_phone_converter.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/number_helper/number_to_phone_converter.rb