container_number_validator 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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +1 -0
- data/container_number_validator.gemspec +25 -0
- data/container_number_validator.iml +14 -0
- data/lib/container_number_validator/adapters/active_model.rb +11 -0
- data/lib/container_number_validator/version.rb +3 -0
- data/lib/container_number_validator.rb +64 -0
- data/spec/container_number_validator/adapters/active_model_spec.rb +18 -0
- data/spec/container_number_validator/container_number_validator_spec.rb +46 -0
- data/spec/spec_helper.rb +39 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 09d51e852c15fc8b6107f0c5f39bff77f983777f
|
4
|
+
data.tar.gz: 4c910afd9c3e6b1c712fab7cb94bce20c0b0ca94
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f7dbd3f2dda073ef24b6f63691d9ec37fbfacb324b94212ad20d8d5e27555e09ca39b333355bc36190826634e25b0b8379e37df1cad6762189a48994045604f
|
7
|
+
data.tar.gz: 3ad5790ac27db6afc84bff75c66bf68c7c6c951ce089f4b09c93e6003436e3f2ec0689e551365b4d32dba164676c6c08a3c8af31f2b083165894af0049778401
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.0.0-p247
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Niels Stevens
|
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,70 @@
|
|
1
|
+
# Container Number Validator
|
2
|
+
|
3
|
+
This gem allows you to easily validate container number fields to be valid
|
4
|
+
container numbers.
|
5
|
+
|
6
|
+
It validates Container Number according to [ISO_6346](http://en.wikipedia.org/wiki/ISO_6346)
|
7
|
+
|
8
|
+
NYKU5824120
|
9
|
+
|
10
|
+
Also supports format where checkdigit is seperated with a dash like this
|
11
|
+
|
12
|
+
NYKU582412-0
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'container_number_validator'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install container_number_validator
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
ContainerNumberValidator.validate('NYKU582412-0')
|
31
|
+
#=> true
|
32
|
+
|
33
|
+
ContainerNumberValidator.validate('NYKU582412-3')
|
34
|
+
#=> false
|
35
|
+
|
36
|
+
### Rails 3.x / 4.x
|
37
|
+
|
38
|
+
You can use ContainerNumberValidator with any ActiveModel class, including
|
39
|
+
ActiveRecord models:
|
40
|
+
|
41
|
+
class Container < ActiveRecord::Base
|
42
|
+
validates :container_no, container_number: true
|
43
|
+
end
|
44
|
+
|
45
|
+
### Edge cases
|
46
|
+
|
47
|
+
* When no container number is present (or set to `nil`), all container numbers are considered **INVALID**.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
### Wishlist
|
52
|
+
|
53
|
+
Nothing specific in mind yet ;-)
|
54
|
+
|
55
|
+
If you're unsure what to contribute, contact me. :-)
|
56
|
+
|
57
|
+
### How to contribute
|
58
|
+
|
59
|
+
I prefer a pull request with added, but failing, specs to code without
|
60
|
+
specs.
|
61
|
+
|
62
|
+
1. Fork it
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create new Pull Request
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
See [LICENSE.txt](https://github.com/kabisaict/container_number_validator/blob/master/LICENSE.txt)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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 'container_number_validator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'container_number_validator'
|
8
|
+
spec.version = ContainerNumberValidator::VERSION
|
9
|
+
spec.authors = ['Niels Stevens', 'Michel De Graaf']
|
10
|
+
spec.email = ['niels@kabisa.nl', 'michel@kabisa.nl']
|
11
|
+
spec.description = %q{This gems validates Sea Container Numbers. It adds a Rails validator for this to your project.}
|
12
|
+
spec.summary = %q{Validate Sea Container numbers}
|
13
|
+
spec.homepage = 'https://github.com/kabisaict/container_number_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,14 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<module type="RUBY_MODULE" version="4">
|
3
|
+
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
4
|
+
<exclude-output />
|
5
|
+
<content url="file://$MODULE_DIR$">
|
6
|
+
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
7
|
+
</content>
|
8
|
+
<orderEntry type="inheritedJdk" />
|
9
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
10
|
+
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, RVM: ruby-2.0.0-p247 [global]) [gem]" level="application" />
|
11
|
+
<orderEntry type="library" scope="PROVIDED" name="rake (v10.1.0, RVM: ruby-2.0.0-p247 [global]) [gem]" level="application" />
|
12
|
+
</component>
|
13
|
+
</module>
|
14
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Validations
|
5
|
+
class ContainerNumberValidator < ::ActiveModel::EachValidator
|
6
|
+
def validate_each(record, attribute, value)
|
7
|
+
record.errors.add(attribute, :invalid_container_number, message: 'is invalid') unless ::ContainerNumberValidator.validate(value)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module ContainerNumberValidator
|
2
|
+
|
3
|
+
LETTERS = {
|
4
|
+
'A' => 10,
|
5
|
+
'B' => 12,
|
6
|
+
'C' => 13,
|
7
|
+
'D' => 14,
|
8
|
+
'E' => 15,
|
9
|
+
'F' => 16,
|
10
|
+
'G' => 17,
|
11
|
+
'H' => 18,
|
12
|
+
'I' => 19,
|
13
|
+
'J' => 20,
|
14
|
+
'K' => 21,
|
15
|
+
'L' => 23,
|
16
|
+
'M' => 24,
|
17
|
+
'N' => 25,
|
18
|
+
'O' => 26,
|
19
|
+
'P' => 27,
|
20
|
+
'Q' => 28,
|
21
|
+
'R' => 29,
|
22
|
+
'S' => 30,
|
23
|
+
'T' => 31,
|
24
|
+
'U' => 32,
|
25
|
+
'V' => 34,
|
26
|
+
'W' => 35,
|
27
|
+
'X' => 36,
|
28
|
+
'Y' => 37,
|
29
|
+
'Z' => 38
|
30
|
+
}.freeze
|
31
|
+
|
32
|
+
WEIGHTS = %w{1 2 4 8 16 32 64 128 256 512}.freeze
|
33
|
+
|
34
|
+
def validate(container_no)
|
35
|
+
return false unless container_no.to_s =~ /^[a-zA-Z]{4}\d{6}\-?\d$/
|
36
|
+
|
37
|
+
checksum = container_no[-1, 1].to_i
|
38
|
+
number = container_no[0..-1]
|
39
|
+
|
40
|
+
calculate_checksum(number) == checksum
|
41
|
+
end
|
42
|
+
module_function :validate
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def calculate_checksum(container_no)
|
47
|
+
chars = container_no.chars
|
48
|
+
digits, weights = [], []
|
49
|
+
|
50
|
+
chars[0..3].each { |char| digits << LETTERS[char.upcase] }
|
51
|
+
chars[4..10].each { |char| digits << char }
|
52
|
+
|
53
|
+
digits.each_with_index do |digit, i|
|
54
|
+
weights[i] = digit.to_i * WEIGHTS[i].to_i
|
55
|
+
end
|
56
|
+
|
57
|
+
weights.reduce(:+) % 11
|
58
|
+
end
|
59
|
+
module_function :calculate_checksum
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'container_number_validator/version'
|
64
|
+
require 'container_number_validator/adapters/active_model' if defined?(ActiveModel)
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
if defined?(ActiveModel)
|
4
|
+
|
5
|
+
class Container < ModelBase
|
6
|
+
validates :container_no, container_number: true
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Container do
|
10
|
+
it 'accepts only valid container numbers' do
|
11
|
+
obj = Container.new(container_no: 'TCLU812975-4')
|
12
|
+
expect(obj).to be_valid
|
13
|
+
|
14
|
+
obj = Container.new(container_no: 'TCLU812975-7')
|
15
|
+
expect(obj).not_to be_valid
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ContainerNumberValidator do
|
4
|
+
|
5
|
+
context 'fails on nil and empty values' do
|
6
|
+
it 'with nil' do
|
7
|
+
expect(subject.validate(nil)).to be_false
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'with empty string' do
|
11
|
+
expect(subject.validate('')).to be_false
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'fails on invalid owner code and category indentifier' do
|
16
|
+
%w{1CLU812975-4 TCLU81A975-4 TCLU81-975-4 T3LU812975-4}.each do |value|
|
17
|
+
it "with #{value}" do
|
18
|
+
expect(subject.validate(value)).to be_false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'fails on invalid checksum' do
|
24
|
+
%w{TCLU812975-1 MOFU587008-2 DRYU413526-6 NYKU561651-8}.each do |value|
|
25
|
+
it "with #{value}" do
|
26
|
+
expect(subject.validate(value)).to be_false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'passes on valid owner code and category identifier' do
|
32
|
+
%w{MOFU587008-7 DRYU413526-5 NYKU644101-8 KKFU137865-2 NYKU561651-2 KKFU760143-9 TCLU250850-8}.each do |value|
|
33
|
+
it "with #{value}" do
|
34
|
+
expect(subject.validate(value)).to be_true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'passes on mixed case valid owner code and category identifier' do
|
40
|
+
%w{TCLU812975-4 TCLU8129754 tclu8129754 tclu812975-4}.each do |value|
|
41
|
+
it "with #{value}" do
|
42
|
+
expect(subject.validate(value)).to be_true
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'active_model'
|
5
|
+
rescue LoadError => err
|
6
|
+
puts 'Running specs without active_model extension'
|
7
|
+
end
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
10
|
+
require 'container_number_validator'
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run :focus
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
18
|
+
|
19
|
+
if defined?(ActiveModel)
|
20
|
+
class ModelBase
|
21
|
+
include ActiveModel::Serialization
|
22
|
+
include ActiveModel::Validations
|
23
|
+
|
24
|
+
attr_accessor :attributes
|
25
|
+
|
26
|
+
def initialize(attributes = {})
|
27
|
+
@attributes = attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
def read_attribute_for_validation(key)
|
31
|
+
@attributes[key.to_sym]
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(method_name, *args, &block)
|
35
|
+
return super unless @attributes.keys.any? { |k| k == method_name }
|
36
|
+
@attributes[method_name.to_sym]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: container_number_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Niels Stevens
|
8
|
+
- Michel De Graaf
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.14.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.14.0
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: activemodel
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 4.0.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 4.0.0
|
70
|
+
description: This gems validates Sea Container Numbers. It adds a Rails validator
|
71
|
+
for this to your project.
|
72
|
+
email:
|
73
|
+
- niels@kabisa.nl
|
74
|
+
- michel@kabisa.nl
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- .rspec
|
81
|
+
- .ruby-version
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- container_number_validator.gemspec
|
87
|
+
- container_number_validator.iml
|
88
|
+
- lib/container_number_validator.rb
|
89
|
+
- lib/container_number_validator/adapters/active_model.rb
|
90
|
+
- lib/container_number_validator/version.rb
|
91
|
+
- spec/container_number_validator/adapters/active_model_spec.rb
|
92
|
+
- spec/container_number_validator/container_number_validator_spec.rb
|
93
|
+
- spec/spec_helper.rb
|
94
|
+
homepage: https://github.com/kabisaict/container_number_validator
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.0.6
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Validate Sea Container numbers
|
118
|
+
test_files:
|
119
|
+
- spec/container_number_validator/adapters/active_model_spec.rb
|
120
|
+
- spec/container_number_validator/container_number_validator_spec.rb
|
121
|
+
- spec/spec_helper.rb
|