license_plate_validator 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +6 -0
- data/gemfiles/activemodel-3-2 +5 -0
- data/gemfiles/activemodel-4 +5 -0
- data/gemfiles/standalone +4 -0
- data/lib/license_plate_validator.rb +39 -0
- data/lib/license_plate_validator/active_model.rb +19 -0
- data/lib/license_plate_validator/locales/en.yml +4 -0
- data/lib/license_plate_validator/locales/nl.yml +4 -0
- data/lib/license_plate_validator/version.rb +3 -0
- data/license_plate_validator.gemspec +25 -0
- data/spec/license_plate_validator/active_model_spec.rb +16 -0
- data/spec/license_plate_validator_spec.rb +56 -0
- data/spec/spec_helper.rb +26 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 612a3486dc6990acf095cf3f62f377c2576d0331
|
4
|
+
data.tar.gz: 07bdfab07e6e9846000e598e8abf38a5e476e9e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65f07d4ac403bb6f5247fb1ff2f150e96505328fc5dc20e282c178a33d76f88e15a6470594deb9b47e38b1da1cfdbbd5f89249314af71f0bfd21ef3c2b333f3e
|
7
|
+
data.tar.gz: 6802d1f908ed1424bbea8fa5cc15d11c4d6543115fddd26ae4de65312b3978f6094165973690c67797d5bd872ddf95b2bd62cb47ca3c668fee2ec667ed0f7bec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ariejan de Vroom
|
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,76 @@
|
|
1
|
+
# License Plate Validator
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ariejan/license_plate_validator.png?branch=master)](https://travis-ci.org/ariejan/license_plate_validator)
|
4
|
+
|
5
|
+
This gem allows you to easily valdiate license plate fields to be valid
|
6
|
+
license plates.
|
7
|
+
|
8
|
+
We're planning on supporting multiple countries, but at this time we can
|
9
|
+
only validate Dutch license plates.
|
10
|
+
|
11
|
+
## Features
|
12
|
+
|
13
|
+
Validate Dutch license plates ("kentekens"), includes all ten common Dutch license
|
14
|
+
plate types.
|
15
|
+
|
16
|
+
Special license plates (like those for the Royal family) are not supported.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
gem 'license_plate_validator'
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install license_plate_validator
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Without ActiveModel or Rails:
|
35
|
+
|
36
|
+
LicensePlateValidator.new("60-NFH-1").valid?
|
37
|
+
#=> true
|
38
|
+
|
39
|
+
Or add it to your Rails 3.x or 4.x project, using ActiveModel:
|
40
|
+
|
41
|
+
class Vehicle < ActiveRecord::Base
|
42
|
+
validates :license_plate_number, license_plate: true
|
43
|
+
end
|
44
|
+
|
45
|
+
## I18n
|
46
|
+
|
47
|
+
Locales for English and Dutch are provided. See [`lib/license_plate_validator/locales`](https://github.com/ariejan/license_plate_validator/tree/master/lib/license_plate_validator/locales)
|
48
|
+
for details.
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
### Wishlist
|
53
|
+
|
54
|
+
I'd like to add the following feature (in no specific order), maybe you can help?
|
55
|
+
|
56
|
+
* Validate uncommon license plates as well (like CD-dd-dd and AA-dd)
|
57
|
+
* Check if characters used are actually allowed.
|
58
|
+
* Output properly formatted license plate numbers
|
59
|
+
* Add support for other countries where possible. Germany, Belgium, France and Poland are high on the list.
|
60
|
+
|
61
|
+
If you're unsure what to contribute, contact me. :-)
|
62
|
+
|
63
|
+
### How to contribute
|
64
|
+
|
65
|
+
I prefer a pull request with added, but failing, specs to code without
|
66
|
+
specs.
|
67
|
+
|
68
|
+
1. Fork it
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create new Pull Request
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
See [LICENSE.txt](https://github.com/ariejan/license_plate_validator/blob/master/LICENSE.txt)
|
data/Rakefile
ADDED
data/gemfiles/standalone
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class LicensePlateValidator
|
2
|
+
|
3
|
+
PATTERNS = [
|
4
|
+
# Netherlands
|
5
|
+
/^([a-zA-Z]{2})-?([0-9]{2})-?([0-9]{2})$/,
|
6
|
+
/^([0-9]{2})-?([0-9]{2})-?([a-zA-Z]{2})$/,
|
7
|
+
/^([0-9]{2})-?([a-zA-Z]{2})-?([0-9]{2})$/,
|
8
|
+
/^([a-zA-Z]{2})-?([0-9]{2})-?([a-zA-Z]{2})$/,
|
9
|
+
/^([a-zA-Z]{2})-?([a-zA-Z]{2})-?([0-9]{2})$/,
|
10
|
+
/^([0-9]{2})-?([a-zA-Z]{2})-?([a-zA-Z]{2})$/,
|
11
|
+
/^([0-9]{2})-?([a-zA-Z]{3})-?([0-9]{1})$/,
|
12
|
+
/^([0-9]{1})-?([a-zA-Z]{3})-?([0-9]{2})$/,
|
13
|
+
/^([a-zA-Z]{2})-?([0-9]{3})-?([a-zA-Z]{1})$/,
|
14
|
+
/^([a-zA-Z]{1})-?([0-9]{3})-?([a-zA-Z]{2})$/
|
15
|
+
].freeze
|
16
|
+
|
17
|
+
def initialize(raw)
|
18
|
+
@raw = normalize(raw || "")
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :raw
|
22
|
+
|
23
|
+
def valid?
|
24
|
+
PATTERNS.any? { |pattern| raw =~ pattern }
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_s
|
28
|
+
raw
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def normalize(input)
|
34
|
+
input.to_s.upcase.strip.gsub(/[^A-Z0-9]/, '')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'license_plate_validator/version'
|
39
|
+
require 'license_plate_validator/active_model' if defined?(ActiveModel)
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class LicensePlateValidator < ::ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
license_plate = ::LicensePlateValidator.new(value.to_s)
|
9
|
+
|
10
|
+
if !license_plate.valid?
|
11
|
+
record.errors.add(attribute, :invalid_license_plate, message: options[:message])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
I18n.load_path += Dir["#{File.dirname(__FILE__)}/locales/*.yml"]
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'license_plate_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "license_plate_validator"
|
8
|
+
spec.version = LicensePlateValidator::VERSION
|
9
|
+
spec.authors = ["Ariejan de Vroom"]
|
10
|
+
spec.email = ["ariejan@ariejan.net"]
|
11
|
+
spec.description = %q{Formatting and content of license plate codes can be validated in some countries. This gem add Rails validators for this to your project.}
|
12
|
+
spec.summary = %q{Validate license plate numbers for countries that support it.}
|
13
|
+
spec.homepage = "https://ariejan.github.io/license_plate_validator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rspec", "~> 2.14.0"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency 'activemodel', '~> 4.0.0'
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined?(ActiveModel)
|
4
|
+
class Vehicle < ModelBase
|
5
|
+
validates :number, license_plate: true
|
6
|
+
end
|
7
|
+
|
8
|
+
describe Vehicle do
|
9
|
+
context "with a valid license plate number" do
|
10
|
+
it "is valid" do
|
11
|
+
obj = Vehicle.new(number: "60-NFH-1")
|
12
|
+
expect(obj).to be_valid
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LicensePlateValidator do
|
4
|
+
it 'should have a version number' do
|
5
|
+
LicensePlateValidator::VERSION.should_not be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#new" do
|
9
|
+
it 'demands one and only one argument' do
|
10
|
+
expect { LicensePlateValidator.new }.to raise_error
|
11
|
+
expect { LicensePlateValidator.new("a", "b") }.to raise_error
|
12
|
+
expect { LicensePlateValidator.new ("a") }.not_to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'normalizes the input string' do
|
16
|
+
expect(LicensePlateValidator.new('60-NFH-1').to_s).to eql('60NFH1')
|
17
|
+
expect(LicensePlateValidator.new('fz-xx-61').to_s).to eql('FZXX61')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "NL - Netherlands" do
|
22
|
+
NL_VALID_SAMPLES = [
|
23
|
+
"FZ-61-24",
|
24
|
+
"24-61-FX",
|
25
|
+
"24-FZ-61",
|
26
|
+
"FZ-61-XX",
|
27
|
+
"FZ-XX-61",
|
28
|
+
"82-HS-JH",
|
29
|
+
"61-NFH-1",
|
30
|
+
"1-KGB-42",
|
31
|
+
"FZ-142-X",
|
32
|
+
"X-721-FZ"
|
33
|
+
]
|
34
|
+
|
35
|
+
NL_INVALID_SAMPLES = [
|
36
|
+
"DEF-123",
|
37
|
+
"123-DEF",
|
38
|
+
"DEFGHI",
|
39
|
+
"123456"
|
40
|
+
]
|
41
|
+
|
42
|
+
NL_VALID_SAMPLES.each do |number|
|
43
|
+
it "accepts '#{number}'" do
|
44
|
+
license = LicensePlateValidator.new(number)
|
45
|
+
expect(license).to be_valid
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
NL_INVALID_SAMPLES.each do |number|
|
50
|
+
it "does not accept '#{number}'" do
|
51
|
+
license = LicensePlateValidator.new(number)
|
52
|
+
expect(license).not_to be_valid
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
begin
|
3
|
+
require 'active_model'
|
4
|
+
rescue LoadError => err
|
5
|
+
puts "Running specs without active_model extension"
|
6
|
+
end
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
9
|
+
require 'license_plate_validator'
|
10
|
+
|
11
|
+
if defined?(ActiveModel)
|
12
|
+
class ModelBase
|
13
|
+
include ActiveModel::Serialization
|
14
|
+
include ActiveModel::Validations
|
15
|
+
|
16
|
+
attr_accessor :attributes
|
17
|
+
|
18
|
+
def initialize(attributes = {})
|
19
|
+
@attributes = attributes
|
20
|
+
end
|
21
|
+
|
22
|
+
def read_attribute_for_validation(key)
|
23
|
+
@attributes[key.to_sym]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: license_plate_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ariejan de Vroom
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.14.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 2.14.0
|
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: activemodel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 4.0.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 4.0.0
|
69
|
+
description: Formatting and content of license plate codes can be validated in some
|
70
|
+
countries. This gem add Rails validators for this to your project.
|
71
|
+
email:
|
72
|
+
- ariejan@ariejan.net
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .rspec
|
79
|
+
- .travis.yml
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- gemfiles/activemodel-3-2
|
85
|
+
- gemfiles/activemodel-4
|
86
|
+
- gemfiles/standalone
|
87
|
+
- lib/license_plate_validator.rb
|
88
|
+
- lib/license_plate_validator/active_model.rb
|
89
|
+
- lib/license_plate_validator/locales/en.yml
|
90
|
+
- lib/license_plate_validator/locales/nl.yml
|
91
|
+
- lib/license_plate_validator/version.rb
|
92
|
+
- license_plate_validator.gemspec
|
93
|
+
- spec/license_plate_validator/active_model_spec.rb
|
94
|
+
- spec/license_plate_validator_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: https://ariejan.github.io/license_plate_validator
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
metadata: {}
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
requirements: []
|
115
|
+
rubyforge_project:
|
116
|
+
rubygems_version: 2.0.3
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Validate license plate numbers for countries that support it.
|
120
|
+
test_files:
|
121
|
+
- spec/license_plate_validator/active_model_spec.rb
|
122
|
+
- spec/license_plate_validator_spec.rb
|
123
|
+
- spec/spec_helper.rb
|