Class: Phonie::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/phonie/formatter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Formatter

Returns a new instance of Formatter.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/phonie/formatter.rb', line 19

def initialize(params)
  @phone_number = params[:phone_number]

  format = params[:format]
  @format = if format.respond_to?(:gsub)
    format
  else
    self.class.named_formats[format]
  end

  raise ArgumentError.new("No valid format provided") if @format.nil?
  raise ArgumentError.new("No valid phone number provided") if @phone_number.nil?
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



17
18
19
# File 'lib/phonie/formatter.rb', line 17

def format
  @format
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



17
18
19
# File 'lib/phonie/formatter.rb', line 17

def phone_number
  @phone_number
end

Class Method Details

.named_formatsObject



33
34
35
# File 'lib/phonie/formatter.rb', line 33

def self.named_formats
  default_named_formats.merge(Phonie.configuration.custom_named_formats)
end

Instance Method Details

#to_sObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/phonie/formatter.rb', line 37

def to_s
  pn = phone_number

  format.gsub("%c", pn.country_code.to_s).
    gsub("%a", pn.area_code.to_s).
    gsub("%A", pn.area_code_long.to_s).
    gsub("%n", pn.number.to_s).
    gsub("%f", pn.number1.to_s).
    gsub("%l", pn.number2.to_s).
    gsub("%x", pn.extension.to_s).
    gsub("%X", pn.extension_with_prefix.to_s)
end