Class: Phonie::Formatter
- Inherits:
-
Object
- Object
- Phonie::Formatter
- Defined in:
- lib/phonie/formatter.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#phone_number ⇒ Object
readonly
Returns the value of attribute phone_number.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params) ⇒ Formatter
constructor
A new instance of Formatter.
- #to_s ⇒ Object
Constructor Details
#initialize(params) ⇒ Formatter
Returns a new instance of Formatter.
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
#format ⇒ Object (readonly)
Returns the value of attribute format.
17 18 19 |
# File 'lib/phonie/formatter.rb', line 17 def format @format end |
#phone_number ⇒ Object (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_formats ⇒ Object
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_s ⇒ Object
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 |