isaf_id_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 +38 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/isaf_id_validator.gemspec +22 -0
- data/lib/isaf_id_validator.rb +22 -0
- data/project.sublime-project +13 -0
- data/spec/isaf_id_validator_spec.rb +98 -0
- data/spec/spec_helper.rb +20 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 96d8ad6c22ac5e40cacbdc20f6a5f8d56da5d79d
|
4
|
+
data.tar.gz: 672e8a7b7b060d9ef55ad26540850dcbf7434b19
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70bd52901497f5d2d44cdf9c4a10d1828e2fef5cc9c6f3a6ea6bfb435b99ad535756fe9583cf7a34657f585a6d1ec29619dbcb20b6982d97c826c10ac8480f83
|
7
|
+
data.tar.gz: d87b98ee9e49f3560bcded5ac0e14f0381081c5634885e376bd5bd8348e3220acab2ae5f8cb9de2430329046f5ab0c86bdd599f0fbe601f30e3ca3fd7865fc3e
|
data/.gitignore
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/test/tmp/
|
9
|
+
/test/version_tmp/
|
10
|
+
/tmp/
|
11
|
+
|
12
|
+
## Specific to RubyMotion:
|
13
|
+
.dat*
|
14
|
+
.repl_history
|
15
|
+
build/
|
16
|
+
|
17
|
+
## Documentation cache and generated files:
|
18
|
+
/.yardoc/
|
19
|
+
/_yardoc/
|
20
|
+
/doc/
|
21
|
+
/rdoc/
|
22
|
+
|
23
|
+
## Environment normalisation:
|
24
|
+
/.bundle/
|
25
|
+
/vendor/bundle
|
26
|
+
/lib/bundler/man/
|
27
|
+
|
28
|
+
# for a library or gem, you might want to ignore these files since the code is
|
29
|
+
# intended to run in multiple environments; otherwise, check them in:
|
30
|
+
Gemfile.lock
|
31
|
+
.ruby-version
|
32
|
+
.ruby-gemset
|
33
|
+
|
34
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
35
|
+
.rvmrc
|
36
|
+
|
37
|
+
# Sublime Text workspace
|
38
|
+
project.sublime-workspace
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Alexander Lazarov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Readme
|
2
|
+
|
3
|
+
This gem is validator for [ISAF Sailor ID](http://www.sailing.org/isafsailor).
|
4
|
+
It works with `Rails 4` but it should work with `Rails 3` too.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Add to your Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'isaf_id_validator'
|
12
|
+
```
|
13
|
+
|
14
|
+
Run:
|
15
|
+
|
16
|
+
```bash
|
17
|
+
bundle install
|
18
|
+
```
|
19
|
+
|
20
|
+
Then add the following to your model:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
validates :isaf_id_attribute, isaf_id: true
|
24
|
+
```
|
25
|
+
|
26
|
+
## Testing
|
27
|
+
|
28
|
+
Use `rspec` to run the tests.
|
29
|
+
|
30
|
+
```bash
|
31
|
+
bundle exec rspec
|
32
|
+
```
|
33
|
+
|
34
|
+
## Credit
|
35
|
+
|
36
|
+
Used [`email_validator`](https://github.com/balexand/email_validator) as a
|
37
|
+
template for this.
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Your contributions are welcome. Please fork the project, make a new branch and
|
42
|
+
send me a pull request.
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'isaf_id_validator'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.authors = ['Alexander Lazarov']
|
5
|
+
s.description = 'An ISAF ID validator for Rails 4. See homepage for details: https://github.com/alexander-lazarov/isaf_id_validator'
|
6
|
+
s.email = 'alexander.lazaroff@gmail.com'
|
7
|
+
s.extra_rdoc_files = [ 'LICENSE' ]
|
8
|
+
s.files = `git ls-files`.split("\n")
|
9
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
10
|
+
|
11
|
+
s.homepage = 'https://github.com/alexander-lazarov/isaf_id_validator'
|
12
|
+
s.require_paths = %w(lib)
|
13
|
+
s.summary = 'An ISAF ID validator for Rails 4.'
|
14
|
+
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.add_dependency('activemodel', '>= 0')
|
18
|
+
s.add_development_dependency('rubysl', '~> 2.0') if RUBY_ENGINE == 'rbx'
|
19
|
+
|
20
|
+
s.add_development_dependency('rake')
|
21
|
+
s.add_development_dependency('rspec', '>= 0')
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class IsafIdValidator < ActiveModel::EachValidator
|
2
|
+
@@default_options = {}
|
3
|
+
|
4
|
+
def self.valid?(value, options = {})
|
5
|
+
value = value.to_s.upcase
|
6
|
+
regexp = /^([A-Z]{3})([A-Z]{2})([1-9][0-9]*)$/
|
7
|
+
|
8
|
+
!!regexp.match(value)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.default_options
|
12
|
+
@@default_options
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate_each(record, attribute, value)
|
16
|
+
options = @@default_options.merge(self.options)
|
17
|
+
|
18
|
+
unless self.class.valid?(value, self.options)
|
19
|
+
record.errors.add(attribute, options[:message] || :invalid)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
class TestUser < TestModel
|
5
|
+
validates :isaf_id, isaf_id: true
|
6
|
+
end
|
7
|
+
|
8
|
+
class TestUserAllowsNil < TestModel
|
9
|
+
validates :isaf_id, isaf_id: { allow_nil: true }
|
10
|
+
end
|
11
|
+
|
12
|
+
class TestUserAllowsNilFalse < TestModel
|
13
|
+
validates :isaf_id, isaf_id: { allow_nil: false}
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestUserWithMessage < TestModel
|
17
|
+
validates :isaf_id, isaf_id: { message: 'this is not ok'}
|
18
|
+
end
|
19
|
+
|
20
|
+
describe IsafIdValidator do
|
21
|
+
|
22
|
+
describe 'validation' do
|
23
|
+
context 'given the valid IDs' do
|
24
|
+
[
|
25
|
+
'BULAL1',
|
26
|
+
'AUSMB11',
|
27
|
+
'bulal1',
|
28
|
+
'BulAl2'
|
29
|
+
].each do |isaf_id|
|
30
|
+
|
31
|
+
it "#{isaf_id.inspect} should be valid" do
|
32
|
+
expect(TestUser.new(isaf_id: isaf_id)).to be_valid
|
33
|
+
end
|
34
|
+
|
35
|
+
it "#{isaf_id.inspect} should pass the class tester" do
|
36
|
+
expect(IsafIdValidator.valid?(isaf_id)).to eq(true)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "given the invalid IDs" do
|
42
|
+
[
|
43
|
+
'asdffg',
|
44
|
+
'BULAL0',
|
45
|
+
'BULAL01',
|
46
|
+
'BUAL01',
|
47
|
+
'BUlAL1A',
|
48
|
+
'АСДФГ1'
|
49
|
+
].each do |isaf_id|
|
50
|
+
|
51
|
+
it "#{isaf_id.inspect} should not be valid" do
|
52
|
+
expect(TestUser.new(:isaf_id => isaf_id)).not_to be_valid
|
53
|
+
end
|
54
|
+
|
55
|
+
it "#{isaf_id.inspect} should fail the class tester" do
|
56
|
+
expect(IsafIdValidator.valid?(isaf_id)).to be_falsy
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'ID nil' do
|
63
|
+
[nil].each do |isaf_id|
|
64
|
+
it "#{isaf_id.inspect} should not be valid" do
|
65
|
+
expect(TestUser.new(:isaf_id => isaf_id)).not_to be_valid
|
66
|
+
end
|
67
|
+
|
68
|
+
it "#{isaf_id.inspect} should fail the class tester" do
|
69
|
+
expect(IsafIdValidator.valid?(isaf_id)).to be_falsy
|
70
|
+
end
|
71
|
+
|
72
|
+
it "#{isaf_id.inspect} should not be valid" do
|
73
|
+
expect(TestUserAllowsNil.new(:isaf_id => isaf_id)).to be_valid
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "error messages" do
|
80
|
+
context "when the message is not defined" do
|
81
|
+
subject { TestUser.new isaf_id: 'invalid id' }
|
82
|
+
before { subject.valid? }
|
83
|
+
|
84
|
+
it "should add the default message" do
|
85
|
+
expect(subject.errors[:isaf_id]).to include "is invalid"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when the message is defined" do
|
90
|
+
subject { TestUserWithMessage.new isaf_id: 'invalid id' }
|
91
|
+
before { subject.valid? }
|
92
|
+
|
93
|
+
it "should add the customized message" do
|
94
|
+
expect(subject.errors[:isaf_id]).to include 'this is not ok'
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rspec'
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require 'isaf_id_validator'
|
9
|
+
|
10
|
+
class TestModel
|
11
|
+
include ActiveModel::Validations
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
@attributes = attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
def read_attribute_for_validation(key)
|
18
|
+
@attributes[key]
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: isaf_id_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Lazarov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-11 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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
|
+
description: 'An ISAF ID validator for Rails 4. See homepage for details: https://github.com/alexander-lazarov/isaf_id_validator'
|
56
|
+
email: alexander.lazaroff@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files:
|
60
|
+
- LICENSE
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- isaf_id_validator.gemspec
|
67
|
+
- lib/isaf_id_validator.rb
|
68
|
+
- project.sublime-project
|
69
|
+
- spec/isaf_id_validator_spec.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
homepage: https://github.com/alexander-lazarov/isaf_id_validator
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.5
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: An ISAF ID validator for Rails 4.
|
95
|
+
test_files: []
|