validates_zipcode 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d310c9cbfd785c952d2daf6b683f224e6ade763e
4
- data.tar.gz: 17485d2b67e7ff6ee5ecaa9b7fcbb746445ebae4
3
+ metadata.gz: ead02aab62164e7c94c7e9636e6d88b925f78407
4
+ data.tar.gz: e0fcc448e62c75372127adeee6020ab1bb878ca8
5
5
  SHA512:
6
- metadata.gz: 5c40cc987d5e497c36369d8268ec115b0cf93172b1d5137bd054e30ae2202d268ff10ab525b765f11c0c6eb410d152776c95c795d590891f8fb3abd97245d34b
7
- data.tar.gz: fa7f25b102a97c394fa2543d4b624510850b5d56b20fc3d25de13faa4f3a15a0955ad10ad85566a1d38281e68ae52326d50bb8baaf09fbd9c525076142688996
6
+ metadata.gz: 7826163416003ac5cc0271a4ee41e560f5de54e6c8cb44ee469fe66ac9ebf2c87ff5850ec2dfe30829a876d2463b3662ce24c1a3f425d4048ca9359ed2aad680
7
+ data.tar.gz: 4310f9cae117c4d810ed6921c417e77190b061f5e1cc12ef6762186f0b1b6d876c8129aa32d689b069c816eb006f3c229d75c0e58281d9f76a244a33d80aeeb4
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## 0.0.8
5
+
6
+ - Added ValidatesZipcode.valid?(zipcode, country_alpha2) for usage outside models // @dgilperez
@@ -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
@@ -238,7 +238,7 @@ module ValidatesZipcode
238
238
  WS: /\A([a-zA-Z\d\s]){3,}\z/,
239
239
  YE: /\A([a-zA-Z\d\s]){3,}\z/,
240
240
  ZW: /\A([a-zA-Z\d\s]){3,}\z/
241
- }
241
+ }.freeze
242
242
 
243
243
  def regexp_for_country_alpha2(alpha2)
244
244
  alpha2 = alpha2.to_s.upcase.to_sym
@@ -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 = options.fetch(: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 regexp.match(value.to_s)
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
@@ -1,3 +1,3 @@
1
1
  module ValidatesZipcode
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  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
- def zipcode_should_be_valid(record)
156
- ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
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
- expect(record.errors).to be_empty
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
- def zipcode_should_be_invalid(record, zipcode = "invalid_zip")
162
- ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
170
+ def zipcode_should_be_valid(record)
171
+ ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
163
172
 
164
- expect(record.errors.size).to eq 1
165
- end
173
+ expect(record.errors).to be_empty
174
+ end
166
175
 
167
- def build_record(zipcode, country_alpha2)
168
- ValidationDummyClass.new.tap do |object|
169
- object.country_alpha2 = country_alpha2
170
- object.zipcode = zipcode
171
- end
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
- class ValidationDummyClass
175
- include ::ActiveModel::Validations
176
- attr_accessor :zipcode, :country_alpha2
189
+ class ValidationDummyClass
190
+ include ::ActiveModel::Validations
191
+ attr_accessor :zipcode, :country_alpha2
177
192
 
178
- def self.name
179
- 'TestClass'
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.7
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-14 00:00:00.000000000 Z
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