Sha256: 061ca97b2ba9a1864f21ccb783e1e5bd7d3562bd6cb903f49eedee9a7c008542

Contents?: true

Size: 809 Bytes

Versions: 4

Compression:

Stored size: 809 Bytes

Contents

  class String

    # 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)
    def phony_formatted(options = {})
      normalize_country_code = options.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, options.reverse_merge(:format => :national))
    end

    # The bang method
    def phony_formatted!(options = {})
      replace(self.phony_formatted(options))
    end

  end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phony_rails-0.4.2 lib/phony_rails/string_extensions.rb
phony_rails-0.4.1 lib/phony_rails/string_extensions.rb
phony_rails-0.4.0 lib/phony_rails/string_extensions.rb
phony_rails-0.3.2 lib/phony_rails/string_extensions.rb