class PhonyAttribute::PhoneNumber

Constants

BLOCKED_NUMBER

Attributes

extension[RW]
number[RW]
parts[RW]

Public Class Methods

dump(number) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 87
def dump(number)
  return nil if number.nil?
  PhonyAttribute::PhoneNumber(number).format(:db)
end
load(number) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 92
def load(number)
  new(number) unless number.nil? || number.blank?
end
new(number) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 16
def initialize(number)
  @parts = []

  # pre-processing
  @number, @extension = number.split(/[xX]/)
  return unless @number

  @number = "+#{self.class.default_country_code}#{@number}" if self.class.default_country_code && !@number.match(Regexp.new("^\s*[\+#{self.class.default_country_code}]"))
  begin
    @number = Phony.normalize(@number)
    @parts = Phony.split(@number.to_s)
  rescue
    @number = nil
  end
end
parse(number) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 96
def parse(number)
  self.load(number)
end

Public Instance Methods

==(other) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 82
def ==(other)
  to_s == other.to_s
end
area_code() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 69
def area_code
  ac = parts[1]
  ac ? ac : nil # countries that don't have area codes will return false here
end
blocked?() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 53
def blocked?
  BLOCKED_NUMBER == format(:db)
end
country_code() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 65
def country_code
  parts[0]
end
format(fmt = :+, options = {}) click to toggle source
# File lib/phony_attribute/phone_number.rb, line 32
def format(fmt = :+, options = {})
  str = nil
  if self.class.named_formats.has_key?(fmt)
    format = self.class.named_formats[fmt]
    if format.respond_to?(:call)
      str = format.call(self)
    else
      options = options.reverse_merge(self.class.named_formats[fmt])
    end
  else
    options = options.reverse_merge({spaces: '', format: fmt})
  end
  str ||= Phony.formatted(number, options) if number
  str += "x#{extension}" if extension
  str
end
number1() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 74
def number1
  parts[2]
end
number2() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 78
def number2
  parts[3..-1].join("")
end
plausible?() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 61
def plausible?
  Phony.plausible?(@number)
end
to_s() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 49
def to_s
  @to_s ||= format(:us_standard)
end
valid?() click to toggle source
# File lib/phony_attribute/phone_number.rb, line 57
def valid?
  plausible? && @number.length >= 8
end