Class: PhonyAttribute::PhoneNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/phony_attribute/phone_number.rb

Constant Summary collapse

BLOCKED_NUMBER =
"+17378742833"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#extensionObject

Returns the value of attribute extension.



15
16
17
# File 'lib/phony_attribute/phone_number.rb', line 15

def extension
  @extension
end

#numberObject

Returns the value of attribute number.



15
16
17
# File 'lib/phony_attribute/phone_number.rb', line 15

def number
  @number
end

#partsObject

Returns the value of attribute parts.



15
16
17
# File 'lib/phony_attribute/phone_number.rb', line 15

def parts
  @parts
end

Class Method Details

.dump(number) ⇒ Object



87
88
89
90
# 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) ⇒ Object



92
93
94
# File 'lib/phony_attribute/phone_number.rb', line 92

def load(number)
  new(number) unless number.nil? || number.blank?
end

.parse(number) ⇒ Object



96
97
98
# File 'lib/phony_attribute/phone_number.rb', line 96

def parse(number)
  self.load(number)
end

Instance Method Details

#==(other) ⇒ Object



82
83
84
# File 'lib/phony_attribute/phone_number.rb', line 82

def ==(other)
  to_s == other.to_s
end

#area_codeObject



69
70
71
72
# 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?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/phony_attribute/phone_number.rb', line 53

def blocked?
  BLOCKED_NUMBER == format(:db)
end

#country_codeObject



65
66
67
# File 'lib/phony_attribute/phone_number.rb', line 65

def country_code
  parts[0]
end

#format(fmt = :+, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

#number1Object



74
75
76
# File 'lib/phony_attribute/phone_number.rb', line 74

def number1
  parts[2]
end

#number2Object



78
79
80
# File 'lib/phony_attribute/phone_number.rb', line 78

def number2
  parts[3..-1].join("")
end

#plausible?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/phony_attribute/phone_number.rb', line 61

def plausible?
  Phony.plausible?(@number)
end

#to_sObject



49
50
51
# File 'lib/phony_attribute/phone_number.rb', line 49

def to_s
  @to_s ||= format(:us_standard)
end

#valid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/phony_attribute/phone_number.rb', line 57

def valid?
  plausible? && @number.length >= 8
end