phony_rails 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.1.2)
4
+ phony_rails (0.1.5)
5
5
  activerecord (~> 3.0)
6
6
  countries (~> 0.8.2)
7
7
  phony (~> 1.7.4)
data/README.md CHANGED
@@ -24,8 +24,15 @@ Or install it yourself as:
24
24
  In your model add:
25
25
 
26
26
  class SomeModel < ActiveRecord::Base
27
- phony_normalize :phone_number, :default_country_code => 'US' # Normalizes the attribute itself on validation
28
- phony_normalized_method :fax_number # Creates method normalized_fax_number that returns the normalized version of fax_number
27
+
28
+ # Normalizes the attribute itself before
29
+ phony_normalize :phone_number, :default_country_code => 'US'validation
30
+
31
+ # Normalizes attribute before validation and saves into other attribute
32
+ phony_normalize :phone_number, :as => :phone_number_normalized_version, :default_country_code => 'US'
33
+
34
+ # Creates method normalized_fax_number that returns the normalized version of fax_number
35
+ phony_normalized_method :fax_number
29
36
  end
30
37
 
31
38
  The `:default_country_code` options is used to specify a country_code when normalizing.
@@ -42,6 +49,9 @@ In your views use:
42
49
 
43
50
  ## Changelog
44
51
 
52
+ 0.1.6
53
+ * Added :as option to phony_normalize.
54
+
45
55
  0.1.5
46
56
  * some tests and a helper method by ddidier.
47
57
 
data/lib/phony_rails.rb CHANGED
@@ -49,7 +49,8 @@ module PhonyRails
49
49
  options = options.clone
50
50
  options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
51
51
  attributes.each do |attribute|
52
- write_attribute(attribute, PhonyRails.normalize_number(read_attribute(attribute), options))
52
+ attribute_name = options[:as] || attribute
53
+ write_attribute(attribute_name, PhonyRails.normalize_number(read_attribute(attribute), options))
53
54
  end
54
55
  end
55
56
 
@@ -63,9 +64,11 @@ module PhonyRails
63
64
  # It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure
64
65
  # you've geocoded before calling this method!
65
66
  def phony_normalize(*attributes)
66
- options = attributes.last.is_a?(Hash) ? attributes.pop : {}
67
- options.assert_valid_keys :country_code, :default_country_code
67
+ options = attributes.last.is_a?(Hash) ? attributes.pop : {}
68
+ options.assert_valid_keys :country_code, :default_country_code, :as
69
+ raise ArgumentError, ':as option can not be used on phony_normalize with multiple attribute names! (PhonyRails)' if !options[:as].blank? && attributes.size > 1
68
70
  attributes.each do |attribute|
71
+ raise ArgumentError, "No attribute #{attribute} found on #{self.class.name} (PhonyRails)" if not self.attribute_method?(attribute)
69
72
  # Add before validation that saves a normalized version of the phone number
70
73
  self.before_validation do
71
74
  set_phony_normalized_numbers(attributes, options)
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
@@ -55,6 +55,26 @@ describe PhonyRails do
55
55
  end
56
56
  end
57
57
 
58
+ describe 'defining ActiveRecord#phony_normalize' do
59
+ it "should not accept :as option with multiple attribute names" do
60
+ lambda {
61
+ Home.phony_normalize(:phone_number, :phone1_method, :as => 'non_existing_attribute')
62
+ }.should raise_error(ArgumentError)
63
+ end
64
+
65
+ it "should not accept :as option with unexiting attribute name" do
66
+ lambda {
67
+ Home.phony_normalize(:non_existing_attribute, :as => 'non_existing_attribute')
68
+ }.should raise_error(ArgumentError)
69
+ end
70
+
71
+ it "should accept :as option with single attribute name" do
72
+ lambda {
73
+ Home.phony_normalize(:phone_number, :as => 'something_else')
74
+ }.should_not raise_error(ArgumentError)
75
+ end
76
+ end
77
+
58
78
  describe 'using ActiveRecord#phony_normalized_method' do
59
79
  # Following examples have complete number (with country code!)
60
80
  it "should return a normalized version of an attribute" do
@@ -119,8 +139,15 @@ describe PhonyRails do
119
139
  describe 'using ActiveRecord#phony_normalize' do
120
140
  it "should set a normalized version of an attribute" do
121
141
  home = Home.new(:phone_number => "+31-(0)10-1234123")
122
- home.valid?
142
+ home.valid?.should be_true
123
143
  home.phone_number.should eql('31101234123')
124
144
  end
145
+
146
+ it "should set a normalized version of an attribute using :as option" do
147
+ Home.phony_normalize :phone_number, :as => :phone_number_as_normalized
148
+ home = Home.new(:phone_number => "+31-(0)10-1234123")
149
+ home.valid?.should be_true
150
+ home.phone_number_as_normalized.should eql('31101234123')
151
+ end
125
152
  end
126
153
  end
data/spec/spec_helper.rb CHANGED
@@ -14,6 +14,7 @@ ActiveRecord::Schema.define do
14
14
  create_table :homes do |table|
15
15
  table.column :phone_attribute, :string
16
16
  table.column :phone_number, :string
17
+ table.column :phone_number_as_normalized, :string
17
18
  end
18
19
  end
19
20
 
metadata CHANGED
@@ -1,73 +1,72 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: phony_rails
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Joost Hietbrink
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-06-29 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: phony
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
25
19
  - - ~>
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 7
30
- - 4
20
+ - !ruby/object:Gem::Version
31
21
  version: 1.7.4
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: countries
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
38
- requirements:
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.7.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: countries
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
39
35
  - - ~>
40
- - !ruby/object:Gem::Version
41
- segments:
42
- - 0
43
- - 8
44
- - 2
36
+ - !ruby/object:Gem::Version
45
37
  version: 0.8.2
46
38
  type: :runtime
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: activerecord
50
39
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
52
- requirements:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.8.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: activerecord
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
53
51
  - - ~>
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 3
57
- - 0
58
- version: "3.0"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
59
54
  type: :runtime
60
- version_requirements: *id003
61
- description: This Gem adds useful methods to your Rails app to validate, display and save phone numbers.
62
- email:
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ description: This Gem adds useful methods to your Rails app to validate, display and
63
+ save phone numbers.
64
+ email:
63
65
  - joost@joopp.com
64
66
  executables: []
65
-
66
67
  extensions: []
67
-
68
68
  extra_rdoc_files: []
69
-
70
- files:
69
+ files:
71
70
  - Gemfile
72
71
  - Gemfile.lock
73
72
  - LICENSE
@@ -81,37 +80,33 @@ files:
81
80
  - spec/lib/phony_rails_spec.rb
82
81
  - spec/lib/phony_validator_spec.rb
83
82
  - spec/spec_helper.rb
84
- has_rdoc: true
85
83
  homepage: https://github.com/joost/phony_rails
86
84
  licenses: []
87
-
88
85
  post_install_message:
89
86
  rdoc_options: []
90
-
91
- require_paths:
87
+ require_paths:
92
88
  - lib
93
- required_ruby_version: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- segments:
98
- - 0
99
- version: "0"
100
- required_rubygems_version: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- segments:
105
- - 0
106
- version: "0"
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
107
101
  requirements: []
108
-
109
102
  rubyforge_project:
110
- rubygems_version: 1.3.6
103
+ rubygems_version: 1.8.24
111
104
  signing_key:
112
105
  specification_version: 3
113
- summary: This Gem adds useful methods to your Rails app to validate, display and save phone numbers.
114
- test_files:
106
+ summary: This Gem adds useful methods to your Rails app to validate, display and save
107
+ phone numbers.
108
+ test_files:
115
109
  - spec/lib/phony_rails_spec.rb
116
110
  - spec/lib/phony_validator_spec.rb
117
111
  - spec/spec_helper.rb
112
+ has_rdoc: