Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/phony_rails/string_extensions.rb
Instance Method Summary collapse
-
#phony_formatted(options = {}) ⇒ Object
Add a method to the String class so we can easily format phone numbers.
-
#phony_formatted!(options = {}) ⇒ Object
The bang method.
Instance Method Details
#phony_formatted(options = {}) ⇒ Object
Add a method to the String class so we can easily format phone numbers. This enables:
"31612341234".phony_formatted # => '06 12341234'
"31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
To first normalize a String use:
"010-12341234".phony_formatted(:normalize => :NL)
9 10 11 12 13 14 |
# File 'lib/phony_rails/string_extensions.rb', line 9 def phony_formatted( = {}) normalize_country_code = .delete(:normalize) s = (normalize_country_code ? PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s) : self.gsub(/\D/, '')) return if s.blank? Phony.formatted(s, .reverse_merge(:format => :national)) end |
#phony_formatted!(options = {}) ⇒ Object
The bang method
17 18 19 |
# File 'lib/phony_rails/string_extensions.rb', line 17 def phony_formatted!( = {}) replace(self.phony_formatted()) end |