Class: Phones::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/phones/phone.rb

Constant Summary collapse

RESTRICTED_CHARACTERS =
/\-|\(|\)|\.|\s|[a-zA-Z]/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Phone



6
7
8
9
10
# File 'lib/phones/phone.rb', line 6

def initialize(opts = {})
  self.country_code = opts[:country_code]
  self.area_code    = opts[:area_code]
  self.local_code  = opts[:local_code]
end

Instance Attribute Details

#area_codeObject

Returns the value of attribute area_code.



4
5
6
# File 'lib/phones/phone.rb', line 4

def area_code
  @area_code
end

#country_codeObject

Returns the value of attribute country_code.



4
5
6
# File 'lib/phones/phone.rb', line 4

def country_code
  @country_code
end

#local_codeObject

Returns the value of attribute local_code.



4
5
6
# File 'lib/phones/phone.rb', line 4

def local_code
  @local_code
end

Instance Method Details

#prettyObject



31
32
33
34
35
36
37
# File 'lib/phones/phone.rb', line 31

def pretty
  if self.united_states?
    "(#{self.area_code}) #{self.local_code[0..2]}-#{self.local_code[3..-1]}"
  else
    self.to_s
  end
end

#to_phoneObject



43
44
45
# File 'lib/phones/phone.rb', line 43

def to_phone
  self
end

#to_sObject



39
40
41
# File 'lib/phones/phone.rb', line 39

def to_s
  "#{self.country_code}#{self.area_code}#{self.local_code}"
end

#united_states?Boolean



47
48
49
# File 'lib/phones/phone.rb', line 47

def united_states?
  self.country_code == "+1"
end