ico-validator 0.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 +36 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +25 -0
- data/Rakefile +2 -0
- data/ico-validator.gemspec +26 -0
- data/lib/ico-validator/ico_validator.rb +26 -0
- data/lib/ico_validator.rb +4 -0
- data/lib/locale/cs.yml +4 -0
- data/lib/locale/en.yml +4 -0
- data/spec/ico_validator_spec.rb +33 -0
- data/spec/spec_helper.rb +3 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cd7e4d4379f83345f956360e915d5b3dbf453cef
|
4
|
+
data.tar.gz: 04b32bccbfb7c354e4653a570567feb33972ed9f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7dd694325cd1f370ff40feaf1a695cd3ba9c63048118dbdf9d0ce9153468c0d0ce526caca40ab2e402c62aef153175c198f545a2c266a539d2962f90d6efbbc1
|
7
|
+
data.tar.gz: d96f6baf3e691b12688d4fa8037d1195d42f71096cf987f4639436a4f1bd1986bb03209568730344f0c9da98bb1184a8566101fee7c848d4a13de8e645969c2b
|
data/.gitignore
ADDED
@@ -0,0 +1,36 @@
|
|
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
|
+
.rspec
|
12
|
+
Gemfile.lock
|
13
|
+
|
14
|
+
## Specific to RubyMotion:
|
15
|
+
.dat*
|
16
|
+
.repl_history
|
17
|
+
build/
|
18
|
+
|
19
|
+
## Documentation cache and generated files:
|
20
|
+
/.yardoc/
|
21
|
+
/_yardoc/
|
22
|
+
/doc/
|
23
|
+
/rdoc/
|
24
|
+
|
25
|
+
## Environment normalisation:
|
26
|
+
/.bundle/
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Účetnictví on-line
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ico-validator [![Build Status](https://travis-ci.org/ucetnictvi-on-line/ico-validator.svg?branch=master)](https://travis-ci.org/ucetnictvi-on-line/ico-validator) [![Code Climate](https://codeclimate.com/github/ucetnictvi-on-line/ico-validator/badges/gpa.svg)](https://codeclimate.com/github/ucetnictvi-on-line/ico-validator) [![Test Coverage](https://codeclimate.com/github/ucetnictvi-on-line/ico-validator/badges/coverage.svg)](https://codeclimate.com/github/ucetnictvi-on-line/ico-validator)
|
2
|
+
=============
|
3
|
+
|
4
|
+
Rails validator for format of [Czech company identification number - ICO](http://cs.wikipedia.org/wiki/Identifika%C4%8Dn%C3%AD_%C4%8D%C3%ADslo_osoby)
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
ICO validator is implemented as ActiveModel::EachValidator so for validation just add `ico: true` parameter to your attribute validation. If validation doesn't pass it adds `:invalid_format` error on validated attribute.
|
8
|
+
|
9
|
+
### Full Example
|
10
|
+
```ruby
|
11
|
+
attr_accessor :ico
|
12
|
+
validates :ico,
|
13
|
+
ico: true
|
14
|
+
|
15
|
+
```
|
16
|
+
|
17
|
+
### What is validated
|
18
|
+
|
19
|
+
* ICO length - must be exactly 8 characters
|
20
|
+
* ICO must contain only numbers
|
21
|
+
* ICO number must satisfy [last digit control formula ](http://www.cssz.cz/cz/e-podani/pro-vyvojare/definice-druhu-e-podani/p-o/logicke-testy-datove-vety.htm)
|
22
|
+
|
23
|
+
## TODO
|
24
|
+
|
25
|
+
* Add JS validation for ICO fields
|
data/Rakefile
ADDED
@@ -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
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'ico-validator'
|
7
|
+
spec.version = '0.0.1'
|
8
|
+
spec.authors = ['Premysl Donat']
|
9
|
+
spec.email = ['donat@uol.cz']
|
10
|
+
spec.summary = %q{Rails validator for validating format of czech identification number = IC}
|
11
|
+
spec.description = %q{Rails validator for validating format of czech identification number = IC}
|
12
|
+
spec.homepage = 'http://www.uol.cz'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'activemodel', '~> 4.1.0'
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'codeclimate-test-reporter'
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class IcoValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
record.errors.add(attribute, :invalid_format) unless valid_ico?(value.to_s)
|
4
|
+
end
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def valid_ico?(value)
|
9
|
+
value.length == 8 && value.match(/^\d+$/) && last_number_valid?(value)
|
10
|
+
end
|
11
|
+
|
12
|
+
def last_number_valid?(value)
|
13
|
+
value[7].to_i == calculate_valid_last_number(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def calculate_valid_last_number(value)
|
17
|
+
sum = (0..6).inject(0) { |sum, i| sum += value[i].to_i * (8 - i) }
|
18
|
+
mod = sum % 11
|
19
|
+
|
20
|
+
case mod
|
21
|
+
when 0, 10 then 1
|
22
|
+
when 1 then 0
|
23
|
+
else 11 - mod
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/locale/cs.yml
ADDED
data/lib/locale/en.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Validatable
|
4
|
+
include ActiveModel::Validations
|
5
|
+
attr_accessor :ico
|
6
|
+
validates :ico, ico: true
|
7
|
+
end
|
8
|
+
|
9
|
+
describe IcoValidator do
|
10
|
+
subject { Validatable.new }
|
11
|
+
|
12
|
+
['61499609', '25275500', '29233011'].each do |valid_ico|
|
13
|
+
it "ICO #{valid_ico} is valid" do
|
14
|
+
subject.ico = valid_ico
|
15
|
+
|
16
|
+
expect(subject).to be_valid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "ICO is valid with integer value in right format" do
|
21
|
+
subject.ico = 61499609
|
22
|
+
|
23
|
+
expect(subject).to be_valid
|
24
|
+
end
|
25
|
+
|
26
|
+
[nil, '', '1', 123, '1111111X', '00000000', '614996097', '123456789'].each do |invalid_ico|
|
27
|
+
it "ICO is invalid with value #{invalid_ico.inspect}" do
|
28
|
+
subject.ico = invalid_ico
|
29
|
+
expect(subject).not_to be_valid
|
30
|
+
expect(subject.errors.messages).to eq({ico: ["Invalid ICO format."]})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ico-validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Premysl Donat
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-15 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: 4.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.1.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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Rails validator for validating format of czech identification number
|
84
|
+
= IC
|
85
|
+
email:
|
86
|
+
- donat@uol.cz
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- ico-validator.gemspec
|
98
|
+
- lib/ico-validator/ico_validator.rb
|
99
|
+
- lib/ico_validator.rb
|
100
|
+
- lib/locale/cs.yml
|
101
|
+
- lib/locale/en.yml
|
102
|
+
- spec/ico_validator_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
homepage: http://www.uol.cz
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Rails validator for validating format of czech identification number = IC
|
128
|
+
test_files:
|
129
|
+
- spec/ico_validator_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
has_rdoc:
|