123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::NumberHelper::NumberToPhoneConverter

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveSupport::NumberHelper::NumberConverter
Defined in: activesupport/lib/active_support/number_helper/number_to_phone_converter.rb

Constant Summary

NumberConverter - Inherited

DEFAULTS

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

This class inherits a constructor from ActiveSupport::NumberHelper::NumberConverter

Instance Method Details

#convert

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 9

def convert
  str = country_code(opts[:country_code]).dup
  str << convert_to_phone_number(number.to_s.strip)
  str << phone_ext(opts[:extension])
end

#convert_to_phone_number(number) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 16

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

#convert_with_area_code(number) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 24

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

#convert_without_area_code(number) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 31

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

#country_code(code) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 47

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

#delimiter (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 43

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

#phone_ext(ext) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 51

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

#regexp_pattern(default_pattern) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 55

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

#start_with_delimiter?(number) ⇒ Boolean (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/number_helper/number_to_phone_converter.rb', line 39

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