validates_zipcode 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/lib/validates_zipcode.rb +4 -0
- data/lib/validates_zipcode/cldr_regex_collection.rb +1 -1
- data/lib/validates_zipcode/validator.rb +2 -6
- data/lib/validates_zipcode/version.rb +1 -1
- data/lib/validates_zipcode/zipcode.rb +22 -0
- data/spec/validates_zipcode_spec.rb +32 -18
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ead02aab62164e7c94c7e9636e6d88b925f78407
|
4
|
+
data.tar.gz: e0fcc448e62c75372127adeee6020ab1bb878ca8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7826163416003ac5cc0271a4ee41e560f5de54e6c8cb44ee469fe66ac9ebf2c87ff5850ec2dfe30829a876d2463b3662ce24c1a3f425d4048ca9359ed2aad680
|
7
|
+
data.tar.gz: 4310f9cae117c4d810ed6921c417e77190b061f5e1cc12ef6762186f0b1b6d876c8129aa32d689b069c816eb006f3c229d75c0e58281d9f76a244a33d80aeeb4
|
data/CHANGELOG.md
ADDED
data/lib/validates_zipcode.rb
CHANGED
@@ -3,6 +3,10 @@ require "validates_zipcode/cldr_regex_collection"
|
|
3
3
|
require "validates_zipcode/validator"
|
4
4
|
require "validates_zipcode/helper_methods"
|
5
5
|
require "validates_zipcode/version"
|
6
|
+
require "validates_zipcode/zipcode"
|
6
7
|
|
7
8
|
module ValidatesZipcode
|
9
|
+
def self.valid?(zipcode, country_alpha2)
|
10
|
+
ValidatesZipcode::Zipcode.new(zipcode: zipcode, country_alpha2: country_alpha2).valid?
|
11
|
+
end
|
8
12
|
end
|
@@ -16,10 +16,8 @@ require 'active_model/validator'
|
|
16
16
|
|
17
17
|
module ValidatesZipcode
|
18
18
|
class Validator < ActiveModel::EachValidator
|
19
|
-
include CldrRegexpCollection
|
20
|
-
|
21
19
|
def initialize(options)
|
22
|
-
@country_code
|
20
|
+
@country_code = options.fetch(:country_code) { }
|
23
21
|
@country_code_attribute = options.fetch(:country_code_attribute) { :country_alpha2 }
|
24
22
|
|
25
23
|
super
|
@@ -27,10 +25,8 @@ module ValidatesZipcode
|
|
27
25
|
|
28
26
|
def validate_each(record, attribute, value)
|
29
27
|
alpha2 = @country_code || record.send(@country_code_attribute)
|
30
|
-
regexp = regexp_for_country_alpha2(alpha2)
|
31
|
-
return unless regexp
|
32
28
|
|
33
|
-
unless
|
29
|
+
unless ValidatesZipcode::Zipcode.new(zipcode: value.to_s, country_alpha2: alpha2).valid?
|
34
30
|
record.errors.add(attribute, I18n.t('errors.messages.invalid_zipcode', value: value))
|
35
31
|
end
|
36
32
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ValidatesZipcode
|
2
|
+
class Zipcode
|
3
|
+
include CldrRegexpCollection
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
@zipcode = args.fetch(:zipcode)
|
7
|
+
@country_alpha2 = args.fetch(:country_alpha2)
|
8
|
+
end
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
return true unless regexp
|
12
|
+
!!(regexp =~ @zipcode)
|
13
|
+
end
|
14
|
+
alias_method :validate, :valid?
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def regexp
|
19
|
+
@regexp ||= regexp_for_country_alpha2(@country_alpha2)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -151,32 +151,46 @@ describe ValidatesZipcode, '#validate_each' do
|
|
151
151
|
zipcode_should_be_invalid(record)
|
152
152
|
end
|
153
153
|
end
|
154
|
+
end
|
154
155
|
|
155
|
-
|
156
|
-
|
156
|
+
describe ValidatesZipcode, '.valid?' do
|
157
|
+
context "Spain" do
|
158
|
+
it 'does not add errors with a valid zipcode' do
|
159
|
+
expect(ValidatesZipcode.valid?('93108', 'ES')).to eq(true)
|
160
|
+
end
|
157
161
|
|
158
|
-
|
162
|
+
it 'adds errors with an invalid Zipcode' do
|
163
|
+
['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
|
164
|
+
expect(ValidatesZipcode.valid?(zipcode, 'ES')).to eq(false)
|
165
|
+
end
|
166
|
+
end
|
159
167
|
end
|
168
|
+
end
|
160
169
|
|
161
|
-
|
162
|
-
|
170
|
+
def zipcode_should_be_valid(record)
|
171
|
+
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
|
163
172
|
|
164
|
-
|
165
|
-
|
173
|
+
expect(record.errors).to be_empty
|
174
|
+
end
|
166
175
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
176
|
+
def zipcode_should_be_invalid(record, zipcode = "invalid_zip")
|
177
|
+
ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
|
178
|
+
|
179
|
+
expect(record.errors.size).to eq 1
|
180
|
+
end
|
181
|
+
|
182
|
+
def build_record(zipcode, country_alpha2)
|
183
|
+
ValidationDummyClass.new.tap do |object|
|
184
|
+
object.country_alpha2 = country_alpha2
|
185
|
+
object.zipcode = zipcode
|
172
186
|
end
|
187
|
+
end
|
173
188
|
|
174
|
-
|
175
|
-
|
176
|
-
|
189
|
+
class ValidationDummyClass
|
190
|
+
include ::ActiveModel::Validations
|
191
|
+
attr_accessor :zipcode, :country_alpha2
|
177
192
|
|
178
|
-
|
179
|
-
|
180
|
-
end
|
193
|
+
def self.name
|
194
|
+
'TestClass'
|
181
195
|
end
|
182
196
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validates_zipcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Gil
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- .gitignore
|
78
78
|
- .rspec
|
79
79
|
- .travis.yml
|
80
|
+
- CHANGELOG.md
|
80
81
|
- Gemfile
|
81
82
|
- LICENSE.txt
|
82
83
|
- README.md
|
@@ -87,6 +88,7 @@ files:
|
|
87
88
|
- lib/validates_zipcode/railtie.rb
|
88
89
|
- lib/validates_zipcode/validator.rb
|
89
90
|
- lib/validates_zipcode/version.rb
|
91
|
+
- lib/validates_zipcode/zipcode.rb
|
90
92
|
- spec/spec_helper.rb
|
91
93
|
- spec/validates_zipcode_spec.rb
|
92
94
|
- validates_zipcode.gemspec
|