Sha256: 2acc5cbb9cda11478240166c34778182d285ba9655a087fe3d77bed6f2988ea0

Contents?: true

Size: 1.49 KB

Versions: 23

Compression:

Stored size: 1.49 KB

Contents

module ActiveSupport
  module NumberHelper
    class NumberToPhoneConverter < NumberConverter #:nodoc:
      def convert
        str = country_code(opts[:country_code])
        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

23 entries across 23 versions & 3 rubygems

Version Path
activesupport-5.1.7 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.7.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.6.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.6.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.6 lib/active_support/number_helper/number_to_phone_converter.rb
tdiary-5.0.8 vendor/bundle/gems/activesupport-5.1.5/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.5 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.5.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.4 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.4.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.3 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.3.rc3 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.3.rc2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.3.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.1.2/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.2.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-5.1.0 lib/active_support/number_helper/number_to_phone_converter.rb