Sha256: 05ddc3aa3b0f04538147d02a6c2426722d017d837b663f50175014aa486d3ae5

Contents?: true

Size: 1.23 KB

Versions: 106

Compression:

Stored size: 1.23 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)
          number.gsub!(/(\d{1,3})(\d{3})(\d{4}$)/,"(\\1) \\2#{delimiter}\\3")
          number
        end

        def convert_without_area_code(number)
          number.gsub!(/(\d{0,3})(\d{3})(\d{4})$/,"\\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
    end
  end
end

Version data entries

106 entries across 101 versions & 12 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.11.2 lib/active_support/number_helper/number_to_phone_converter.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/number_helper/number_to_phone_converter.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.11.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.11 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.10 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.10.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.9 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.9.rc2 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.9.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.8 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.8.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.7.1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.7 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.1.16 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.1.16.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
activesupport-4.2.7.rc1 lib/active_support/number_helper/number_to_phone_converter.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/number_helper/number_to_phone_converter.rb