Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added String#phony_formatted and bumped version to 0.5.0.

  • Loading branch information...
commit 2ed1254f871519e542da2be7f02c202aa236e413 1 parent 529e51e
@joost authored
View
2  Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- phony_rails (0.4.2)
+ phony_rails (0.5.0)
activesupport (>= 3.0)
countries (>= 0.8.2)
phony (>= 1.7.7)
View
7 README.md
@@ -100,6 +100,10 @@ To first normalize the String to a certain country use:
<%= "010-12341234".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-') %>
+To return nil when a number is not valid:
+
+ "123".phony_formatted(:strict => true) # => nil
+
You can also use the bang method (phony_formatted!):
number = "010-12341234"
@@ -114,6 +118,9 @@ Say you want to find a record by a phone number. Best is to normalize user input
## Changelog
+0.5.0
+* Added :strict option to String#phony_formatted.
+
0.4.2
* Added @fareastside validation for country_code.
View
3  lib/phony_rails/string_extensions.rb
@@ -6,10 +6,13 @@ class String
# "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
View
2  lib/phony_rails/version.rb
@@ -1,3 +1,3 @@
module PhonyRails
- VERSION = "0.4.2"
+ VERSION = "0.5.0"
end
View
16 spec/lib/phony_rails_spec.rb
@@ -17,6 +17,22 @@
end
+ describe 'with strict option' do
+
+ it "should return nil with non plausible number" do
+ number = '+319090' # not valid
+ Phony.plausible?(number).should be_false
+ number.phony_formatted(:strict => true).should eql(nil)
+ end
+
+ it "should not return nil with plausible number" do
+ number = '+31101234123' # valid
+ Phony.plausible?(number).should be_true
+ number.phony_formatted(:strict => true).should_not eql(nil)
+ end
+
+ end
+
describe 'with normalize option' do
it "should phony_format" do
Please sign in to comment.
Something went wrong with that request. Please try again.