validates_zipcode 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b2d8d493f0f737a570f42fb2f52b2f782920eec
4
+ data.tar.gz: a2845754f6050667fe8acbde8c44096c834693eb
5
+ SHA512:
6
+ metadata.gz: 22ec86183bcb7bfa0631a176f53d924ae8f22362c8f3818492ce537a31af3558b068f05eb84ec68e3092c85ec7246c3e71977a3c9805d3d2ac113270a1464619
7
+ data.tar.gz: e97089e5c1a99b71f0d5e475e328b1eedccb394f84251a4adaf9123483c87628ce65edee9b3db8acf663ecffe2d759cc9106f7f1b0ec4f061e866c563494383d
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validates_zipcode.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 David Gil
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # ValidatesZipcode
2
+
3
+ Add zipcode validation support to Rails (ActiveModel), considering different zipcode country formats.
4
+
5
+ Currently supported country zipcodes are: USA, Canada, Spain, New Zeland, Australia, Colombia and Argentina.
6
+ Any other country's zipcode will validate without errors.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'validates_zipcode'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install validates_zipcode
21
+
22
+ ## Usage
23
+
24
+ validates_zipcode :zipcode
25
+
26
+ validates :zipcode, zipcode: true
27
+
28
+ ``ValidatesZipcode`` expects the model to have an attribute called ``country_alpha2`` to contain the country code.
29
+ You can provide your own country_code using ``:country_code`` option, or specify which attribute contains this information
30
+ using ``:country_code_attribute`` option.
31
+
32
+ validates :zipcode, zipcode: { country_code: :es }
33
+
34
+ validates :zipcode, zipcode: { country_code_attribute: :my_country_code_column }
35
+
36
+ If you need to localize the error message, just add this to your I18n locale file:
37
+
38
+ errors:
39
+ messages:
40
+ invalid_zipcode: Your zipcode error message.
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/[my-github-username]/validates_zipcode/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
49
+
50
+ ## Contributors
51
+
52
+ To see the generous people who have contributed code, take a look at the {contributors list}[http://github.com/dgilperez/validates_zipcode/contributors].
53
+
54
+ ## Maintainers
55
+
56
+ * {David Gil}[http://github.com/dgilperez]
57
+
58
+ ## License
59
+
60
+ Copyright (c) 2014 David Gil Pérez, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ module ActiveModel
2
+ module Validations
3
+ module HelperMethods
4
+ def validates_zipcode(*attr_names)
5
+ validates_with ZipcodeValidator, _merge_attributes(attr_names)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module ValidatesZipcode
2
+ class Railtie < Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ # Usage:
2
+ #
3
+ # class User < ActiveModel
4
+ # validates :postalcode, zipcode: true
5
+ #
6
+ # # Required country_alpha2 attribute
7
+ # def country_alpha2
8
+ # country.alpha2
9
+ # end
10
+ # end
11
+ #
12
+
13
+ require 'active_model'
14
+ require 'active_model/validator'
15
+
16
+ module ValidatesZipcode
17
+ class Validator < ActiveModel::EachValidator
18
+ # using country alpha2 code as key
19
+ ZIPCODES_REGEX = {
20
+ ES: /\A\d{5}\z/,
21
+ AR: /\A([A-HJ-TP-Z]{1}\d{4}[A-Z]{3}|[a-z]{1}\d{4}[a-hj-tp-z]{3})\z/,
22
+ CL: /\A[0-9]{3}[-]?[0-9]{4}\z/,
23
+ NZ: /\A\d{4}\z/,
24
+ AU: /\A\d{4}\z/,
25
+ US: /\A\d{5}(-\d{4})?\z/,
26
+ CA: /\A[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}\z/
27
+ # CA: /\A[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d\z/
28
+ }
29
+
30
+ def initialize(options)
31
+ @country_code = options.fetch(:country_code) { }
32
+ @country_code_attribute = options.fetch(:country_code_attribute) { :country_alpha2 }
33
+
34
+ super
35
+ end
36
+
37
+ def validate_each(record, attribute, value)
38
+ alpha2 = @country_code || record.send(@country_code_attribute)
39
+ alpha2 = alpha2.to_s.upcase.to_sym
40
+ regexp = ZIPCODES_REGEX[alpha2]
41
+ return unless regexp
42
+
43
+ unless regexp.match(value.to_s)
44
+ record.errors.add(attribute, I18n.t('errors.messages.invalid_zipcode', value: value))
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ ActiveModel::Validations::ZipcodeValidator = ValidatesZipcode::Validator
@@ -0,0 +1,3 @@
1
+ module ValidatesZipcode
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "validates_zipcode/railtie" if defined?(Rails)
2
+ require "validates_zipcode/validator"
3
+ require "validates_zipcode/helper_methods"
4
+ require "validates_zipcode/version"
5
+
6
+ module ValidatesZipcode
7
+ end
@@ -0,0 +1 @@
1
+ require 'validates_zipcode'
@@ -0,0 +1,132 @@
1
+ require 'spec_helper'
2
+
3
+ describe ValidatesZipcode, '#validate_each' do
4
+ context "Spain" do
5
+ it 'does not add errors with a valid zipcode' do
6
+ record = build_record('93108', "ES")
7
+ zipcode_should_be_valid(record)
8
+ end
9
+
10
+ it 'adds errors with an invalid Zipcode' do
11
+ ['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
12
+ record = build_record(zipcode, 'ES')
13
+ zipcode_should_be_invalid(record, zipcode)
14
+ end
15
+ end
16
+ end
17
+
18
+ context "USA" do
19
+ it 'does not add errors with a valid zipcode' do
20
+ record = build_record('93108', "US")
21
+ zipcode_should_be_valid(record)
22
+ [12345, '12345', '12345-1234'].each do |zipcode|
23
+ record = build_record(zipcode, 'US')
24
+ zipcode_should_be_valid(record)
25
+ end
26
+ end
27
+
28
+ it 'adds errors with an invalid Zipcode' do
29
+ ['1234', '12345-12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
30
+ record = build_record(zipcode, 'US')
31
+ zipcode_should_be_invalid(record, zipcode)
32
+ end
33
+ end
34
+ end
35
+
36
+ context "Argentina" do
37
+ it 'does not add errors with a valid zipcode' do
38
+ record = build_record('C1424CHN', "AR")
39
+ zipcode_should_be_valid(record)
40
+ end
41
+
42
+ it 'adds errors with an invalid Zipcode' do
43
+ ['1234', '12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
44
+ record = build_record(zipcode, 'AR')
45
+ zipcode_should_be_invalid(record, zipcode)
46
+ end
47
+ end
48
+ end
49
+
50
+ context "Chile" do
51
+ it 'does not add errors with a valid zipcode' do
52
+ ['123-1233', '000-0000', '1234567'].each do |zipcode|
53
+ record = build_record(zipcode, "CL")
54
+ zipcode_should_be_valid(record)
55
+ end
56
+ end
57
+
58
+ it 'adds errors with an invalid Zipcode' do
59
+ ['1234', '12345', 'D0D0D0', 'invalid_zip', 'C1424CHN', '122-12345'].each do |zipcode|
60
+ record = build_record(zipcode, 'CL')
61
+ zipcode_should_be_invalid(record, zipcode)
62
+ end
63
+ end
64
+ end
65
+
66
+ context "Australia and New Zeland" do
67
+ it 'does not add errors with a valid zipcode' do
68
+ ["AU", "NZ"].each do |code|
69
+ record = build_record('9310', code)
70
+ zipcode_should_be_valid(record)
71
+ end
72
+ end
73
+
74
+ it 'adds errors with an invalid Zipcode' do
75
+ ["AU", "NZ"].each do |code|
76
+ ['C1424CHN', '12345', 'invalid_zip'].each do |zipcode|
77
+ record = build_record(zipcode, code)
78
+ zipcode_should_be_invalid(record, zipcode)
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ context "Canada" do
85
+ it 'does not add errors with a valid zipcode' do
86
+ record = build_record('A1J2Z9', 'CA')
87
+ zipcode_should_be_valid(record)
88
+ end
89
+
90
+ it 'adds errors with an invalid Zipcode' do
91
+ ['C1424CHN', '12345', 'D0D0D0', 'invalid_zip'].each do |zipcode|
92
+ record = build_record(zipcode, 'CA')
93
+ zipcode_should_be_invalid(record, zipcode)
94
+ end
95
+ end
96
+ end
97
+
98
+ context "unknown country" do
99
+ it 'does not add errors with a any zipcode' do
100
+ record = build_record('A1J2Z9', 'ZZ')
101
+ zipcode_should_be_valid(record)
102
+ end
103
+ end
104
+
105
+ def zipcode_should_be_valid(record)
106
+ ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
107
+
108
+ expect(record.errors).to be_empty
109
+ end
110
+
111
+ def zipcode_should_be_invalid(record, zipcode = "invalid_zip")
112
+ ValidatesZipcode::Validator.new(attributes: :zipcode).validate(record)
113
+
114
+ expect(record.errors.size).to eq 1
115
+ end
116
+
117
+ def build_record(zipcode, country_alpha2)
118
+ ValidationDummyClass.new.tap do |object|
119
+ object.country_alpha2 = country_alpha2
120
+ object.zipcode = zipcode
121
+ end
122
+ end
123
+
124
+ class ValidationDummyClass
125
+ include ::ActiveModel::Validations
126
+ attr_accessor :zipcode, :country_alpha2
127
+
128
+ def self.name
129
+ 'TestClass'
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'validates_zipcode/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "validates_zipcode"
8
+ s.version = ValidatesZipcode::VERSION
9
+ s.authors = ["David Gil"]
10
+ s.email = ["dgilperez@gmail.com"]
11
+ s.summary = %q{Localizable zipcode validation for Rails.}
12
+ s.description = %q{Adds zipcode validation methods to ActiveModel considering different country zipcode formats.}
13
+ s.homepage = "http://github.com/dgilperez/validates_zipcode"
14
+ s.license = "MIT"
15
+
16
+ s.files = `git ls-files -z`.split("\x0")
17
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'activemodel', '>= 3.2.0'
22
+
23
+ s.add_development_dependency "bundler", "~> 1.6"
24
+ s.add_development_dependency "rake"
25
+ s.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_zipcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - David Gil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Adds zipcode validation methods to ActiveModel considering different
70
+ country zipcode formats.
71
+ email:
72
+ - dgilperez@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/validates_zipcode.rb
84
+ - lib/validates_zipcode/helper_methods.rb
85
+ - lib/validates_zipcode/railtie.rb
86
+ - lib/validates_zipcode/validator.rb
87
+ - lib/validates_zipcode/version.rb
88
+ - spec/spec_helper.rb
89
+ - spec/validates_zipcode_spec.rb
90
+ - validates_zipcode.gemspec
91
+ homepage: http://github.com/dgilperez/validates_zipcode
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Localizable zipcode validation for Rails.
115
+ test_files:
116
+ - spec/spec_helper.rb
117
+ - spec/validates_zipcode_spec.rb