Sha256: 7629921dd58d5670cbb8fe0e0787b307cc262aae98d1ccf912bfbe41bb530607

Contents?: true

Size: 1005 Bytes

Versions: 9

Compression:

Stored size: 1005 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)
    # To return nil when a number is not correct (checked using Phony.plausible?) use
    #   "010-12341234".phony_formatted(strict: true)
    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?
      return if options[:strict] && !Phony.plausible?(s)
      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

9 entries across 9 versions & 1 rubygems

Version Path
phony_rails-0.8.0 lib/phony_rails/string_extensions.rb
phony_rails-0.7.3 lib/phony_rails/string_extensions.rb
phony_rails-0.7.2 lib/phony_rails/string_extensions.rb
phony_rails-0.7.1 lib/phony_rails/string_extensions.rb
phony_rails-0.7.0 lib/phony_rails/string_extensions.rb
phony_rails-0.6.2 lib/phony_rails/string_extensions.rb
phony_rails-0.6.1 lib/phony_rails/string_extensions.rb
phony_rails-0.6.0 lib/phony_rails/string_extensions.rb
phony_rails-0.5.0 lib/phony_rails/string_extensions.rb