phony_rails 0.1.12 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,9 +1,14 @@
1
1
  source :rubygems
2
+
3
+ # Preempt the default loading so that we don't get an unqualified Country class imported.
4
+ gem 'countries', :require => 'iso3166'
5
+
2
6
  gemspec # Specify your gem's dependencies in phony_number.gemspec
3
7
 
4
8
  # For testing
5
9
  gem 'sqlite3'
6
10
 
11
+ gem 'rake'
7
12
  gem 'rspec', '~> 2.11.0'
8
13
  gem 'guard', '~> 1.2.0'
9
14
  gem 'guard-bundler', '~> 1.0.0'
@@ -1,30 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.1.10)
5
- activerecord (~> 3.0)
6
- countries (~> 0.8.2)
7
- phony (~> 1.7.7)
4
+ phony_rails (0.1.13)
5
+ activerecord (>= 3.0)
6
+ countries (>= 0.8.2)
7
+ phony (>= 1.7.7)
8
8
 
9
9
  GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
- activemodel (3.2.6)
13
- activesupport (= 3.2.6)
12
+ activemodel (3.2.12)
13
+ activesupport (= 3.2.12)
14
14
  builder (~> 3.0.0)
15
- activerecord (3.2.6)
16
- activemodel (= 3.2.6)
17
- activesupport (= 3.2.6)
15
+ activerecord (3.2.12)
16
+ activemodel (= 3.2.12)
17
+ activesupport (= 3.2.12)
18
18
  arel (~> 3.0.2)
19
19
  tzinfo (~> 0.3.29)
20
- activesupport (3.2.6)
20
+ activesupport (3.2.12)
21
21
  i18n (~> 0.6)
22
22
  multi_json (~> 1.0)
23
23
  arel (3.0.2)
24
- builder (3.0.0)
25
- countries (0.8.2)
26
- currencies (= 0.4.0)
27
- currencies (>= 0.2.0)
24
+ builder (3.0.4)
25
+ countries (0.8.4)
26
+ currencies (>= 0.4.0)
28
27
  currencies (0.4.0)
29
28
  diff-lcs (1.1.3)
30
29
  ffi (1.1.0)
@@ -37,13 +36,14 @@ GEM
37
36
  guard (~> 1.1)
38
37
  guard-rspec (1.2.0)
39
38
  guard (>= 1.1)
40
- i18n (0.6.0)
39
+ i18n (0.6.1)
41
40
  listen (0.4.7)
42
41
  rb-fchange (~> 0.0.5)
43
42
  rb-fsevent (~> 0.9.1)
44
43
  rb-inotify (~> 0.8.8)
45
- multi_json (1.3.6)
46
- phony (1.7.10)
44
+ multi_json (1.6.0)
45
+ phony (1.8.6)
46
+ rake (10.0.3)
47
47
  rb-fchange (0.0.5)
48
48
  ffi
49
49
  rb-fsevent (0.9.1)
@@ -59,17 +59,19 @@ GEM
59
59
  rspec-mocks (2.11.1)
60
60
  sqlite3 (1.3.6)
61
61
  thor (0.15.4)
62
- tzinfo (0.3.33)
62
+ tzinfo (0.3.35)
63
63
 
64
64
  PLATFORMS
65
65
  ruby
66
66
 
67
67
  DEPENDENCIES
68
+ countries
68
69
  growl
69
70
  guard (~> 1.2.0)
70
71
  guard-bundler (~> 1.0.0)
71
72
  guard-rspec (~> 1.2.0)
72
73
  phony_rails!
74
+ rake
73
75
  rb-fsevent
74
76
  rspec (~> 2.11.0)
75
77
  sqlite3
data/README.md CHANGED
@@ -76,7 +76,19 @@ the i18n key is `:improbable_phone`
76
76
 
77
77
  In your views use:
78
78
 
79
- <%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>
79
+ <%= "311012341234".phony_formatted(:format => :international, :spaces => '-') %>
80
+ <%= "+31-10-12341234".phony_formatted(:format => :international, :spaces => '-') %>
81
+ <%= "+31(0)1012341234".phony_formatted(:format => :international, :spaces => '-') %>
82
+
83
+ To first normalize the String to a certain country use:
84
+
85
+ <%= "010-12341234".phony_formatted(normalize => :NL, :format => :international, :spaces => '-') %>
86
+
87
+ You can also use the bang method (phony_formatted!):
88
+
89
+ number = "010-12341234"
90
+ number.phony_formatted!(normalize => :NL, :format => :international)
91
+ number # => "+31 10 12341234"
80
92
 
81
93
  ### Find by normalized number
82
94
 
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -1,5 +1,5 @@
1
1
  require 'phony'
2
- require 'countries'
2
+ require 'iso3166'
3
3
  require 'phony_rails/string_extensions'
4
4
  require 'validators/phony_validator'
5
5
  require 'phony_rails/version'
@@ -25,14 +25,13 @@ module PhonyRails
25
25
  # Add country_number if missing
26
26
  number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
27
27
  end
28
- number = Phony.normalize(number)
28
+ number = Phony.normalize(number) if Phony.plausible?(number)
29
29
  return number.to_s
30
30
  rescue
31
31
  number # If all goes wrong .. we still return the original input.
32
32
  end
33
33
 
34
- # This module is added to AR.
35
- module ActiveRecordExtension
34
+ module Extension
36
35
 
37
36
  def self.extended(base)
38
37
  base.send :include, InstanceMethods
@@ -102,7 +101,13 @@ module PhonyRails
102
101
 
103
102
  end
104
103
 
105
- ActiveRecord::Base.extend PhonyRails::ActiveRecordExtension
104
+ # check whether it is ActiveRecord or Mongoid being used
105
+ ActiveRecord::Base.send :extend, PhonyRails::Extension if defined?(ActiveRecord)
106
+ Mongoid::Document.module_eval do
107
+ def self.included(base)
108
+ base.extend PhonyRails::Extension
109
+ end
110
+ end if defined?(Mongoid)
106
111
 
107
112
  Dir["#{File.dirname(__FILE__)}/phony_rails/locales/*.yml"].each do |file|
108
113
  I18n.load_path << file
@@ -1,4 +1,3 @@
1
-
2
1
  fr:
3
2
  activerecord:
4
3
  errors:
@@ -4,8 +4,18 @@
4
4
  # This enables:
5
5
  # "31612341234".phony_formatted # => '06 12341234'
6
6
  # "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
7
+ # To first normalize a String use:
8
+ # "010-12341234".phony_formatted(:normalize => :NL)
7
9
  def phony_formatted(options = {})
8
- Phony.formatted(self, options.reverse_merge(:format => :national))
10
+ normalize_country_code = options.delete(:normalize)
11
+ s = (normalize_country_code ? PhonyRails.normalize_number(self, :country_code => normalize_country_code.to_s) : self.gsub(/\D/, ''))
12
+ return if s.blank?
13
+ Phony.formatted(s, options.reverse_merge(:format => :national))
14
+ end
15
+
16
+ # The bang method
17
+ def phony_formatted!(options = {})
18
+ replace(self.phony_formatted(options))
9
19
  end
10
20
 
11
21
  end
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.1.12"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,10 +1,70 @@
1
1
  require 'spec_helper'
2
2
  describe PhonyRails do
3
3
 
4
- describe 'String extensions' do
5
- it "should phony_format a String" do
4
+ it "should not pollute the global namespace with a Country class" do
5
+ should_not be_const_defined "Country"
6
+ end
7
+
8
+ describe 'phony_format String extension' do
9
+
10
+ describe 'the bang method phony_formatted!' do
11
+
12
+ it "should change the String using the bang method" do
13
+ s = "0101234123"
14
+ s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
15
+ s.should eql("+31 10 1234123")
16
+ end
17
+
18
+ end
19
+
20
+ describe 'with normalize option' do
21
+
22
+ it "should phony_format" do
23
+ "0101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
24
+ "0101234123".phony_formatted(:normalize => :NL, :format => :international).should eql('+31 10 1234123')
25
+ end
26
+
27
+ it "should not change original String" do
28
+ s = "0101234123"
29
+ s.phony_formatted(:normalize => :NL).should eql('010 1234123')
30
+ s.should eql("0101234123")
31
+ end
32
+
33
+ it "should phony_format String with country code" do
34
+ "31101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
35
+ end
36
+
37
+ it "should phony_format String with country code" do
38
+ "31101234123".phony_formatted(:normalize => :NL).should eql('010 1234123')
39
+ end
40
+
41
+ it "should accept strings with non-digits in it" do
42
+ "+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-').should eql('+31-10-1234123')
43
+ end
44
+
45
+ end
46
+
47
+ it "should not change original String" do
48
+ s = "0101234123"
49
+ s.phony_formatted(:normalize => :NL).should eql('010 1234123')
50
+ s.should eql("0101234123")
51
+ end
52
+
53
+ it "should phony_format a digits string with spaces String" do
54
+ "31 10 1234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
55
+ end
56
+
57
+ it "should phony_format a digits String" do
6
58
  "31101234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-1234123')
7
59
  end
60
+
61
+ it "returns nil if implausible phone" do
62
+ "this is not a phone".phony_formatted.should be_nil
63
+ end
64
+
65
+ it "returns nil on blank string" do
66
+ "".phony_formatted.should be_nil
67
+ end
8
68
  end
9
69
 
10
70
  describe 'PhonyRails#normalize_number' do
@@ -30,6 +90,10 @@ describe PhonyRails do
30
90
  PhonyRails.normalize_number('+31-70-4157134', :country_code => 'NL').should eql('31704157134')
31
91
  PhonyRails.normalize_number('0323-2269497', :country_code => 'BE').should eql('323232269497')
32
92
  end
93
+
94
+ it "should not normalize an implausible number" do
95
+ PhonyRails.normalize_number('01').should eql('01')
96
+ end
33
97
  end
34
98
 
35
99
  describe 'defining ActiveRecord#phony_normalized_method' do
@@ -124,14 +188,14 @@ describe PhonyRails do
124
188
  home.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
125
189
  end
126
190
 
127
- it "should use last passed options" do
191
+ it "should use last passed options" do
128
192
  home = Home.new(:phone1_method => "(030) 8 61 29 06")
129
193
  home.normalized_phone1_method(:country_code => 'NL').should eql('31308612906')
130
194
  home.normalized_phone1_method(:country_code => 'DE').should eql('49308612906')
131
195
  home.normalized_phone1_method(:country_code => nil).should eql('49308612906')
132
196
  end
133
197
 
134
- it "should use last object method" do
198
+ it "should use last object method" do
135
199
  home = Home.new(:phone1_method => "(030) 8 61 29 06")
136
200
  home.country_code = 'NL'
137
201
  home.normalized_phone1_method.should eql('31308612906')
@@ -154,6 +218,6 @@ describe PhonyRails do
154
218
  home = Home.new(:phone_number => "+31-(0)10-1234123")
155
219
  home.valid?.should be_true
156
220
  home.phone_number_as_normalized.should eql('31101234123')
157
- end
221
+ end
158
222
  end
159
- end
223
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-04 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: phony