ico-validator 0.3.1 → 0.4.0
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +0 -3
- data/ico-validator.gemspec +1 -1
- data/lib/ico-validator.rb +0 -5
- data/lib/ico-validator/ico_validation.rb +23 -0
- data/lib/ico-validator/ico_validator.rb +8 -22
- data/spec/ico_validation_spec.rb +21 -0
- data/spec/ico_validator_spec.rb +2 -2
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36c0f4c596aaf1ecaeecad864c17577ee5bcb604
|
4
|
+
data.tar.gz: bc26e9492611e8185559900a8cc753ce560fc6a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af15cfac13cbd03c1effaa3e57e78f8c8f8ba21aa444d0baf7ef54ae38188992f0aa6fd85740818f3bc26e0e5601a1686443af9507c9bf86ca8c335a293c6174
|
7
|
+
data.tar.gz: a69703830b510511847d69872394ad57fa70df20c02839238ce2f8702a8251b9ed96bf79f5207c23fd16c56b5ae5b058f73237dc7e20dee3234b755ed1e2bea0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
data/ico-validator.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'ico-validator'
|
7
|
-
spec.version = '0.
|
7
|
+
spec.version = '0.4.0'
|
8
8
|
spec.authors = ['Premysl Donat']
|
9
9
|
spec.email = ['donat@uol.cz']
|
10
10
|
spec.summary = %q{Rails validator for validating format of czech identification number = IC}
|
data/lib/ico-validator.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
module IcoValidation
|
2
|
+
def self.valid_ico?(ico)
|
3
|
+
ico = ico.to_s
|
4
|
+
ico.length == 8 && ico.match(/^\d+$/) && last_number_valid?(ico)
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def self.last_number_valid?(ico)
|
10
|
+
ico[7].to_i == calculate_valid_last_number(ico)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.calculate_valid_last_number(ico)
|
14
|
+
sum = (0..6).inject(0) { |sum, i| sum + ico[i].to_i * (8 - i) }
|
15
|
+
mod = sum % 11
|
16
|
+
|
17
|
+
case mod
|
18
|
+
when 0, 10 then 1
|
19
|
+
when 1 then 0
|
20
|
+
else 11 - mod
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,26 +1,12 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support/i18n'
|
3
|
+
I18n.load_path << File.dirname(__FILE__) + '/../locale/en.yml'
|
4
|
+
I18n.load_path << File.dirname(__FILE__) + '/../locale/cs.yml'
|
5
|
+
require 'ico-validator/engine'
|
6
|
+
require 'ico-validator/ico_validation'
|
7
|
+
|
1
8
|
class IcoValidator < ActiveModel::EachValidator
|
2
9
|
def validate_each(record, attribute, value)
|
3
|
-
record.errors.add(attribute, :invalid_format) unless valid_ico?(value
|
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
|
10
|
+
record.errors.add(attribute, :invalid_format) unless IcoValidation.valid_ico?(value)
|
25
11
|
end
|
26
12
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe IcoValidation do
|
4
|
+
subject { IcoValidation }
|
5
|
+
|
6
|
+
['61499609', '25275500', '29233011'].each do |valid_ico|
|
7
|
+
it "ICO #{valid_ico} is valid" do
|
8
|
+
expect(subject.valid_ico?(valid_ico)).to eq true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'ICO is valid with integer value in right format' do
|
13
|
+
expect(subject.valid_ico?(61499609)).to eq true
|
14
|
+
end
|
15
|
+
|
16
|
+
[nil, '', '1', 123, '1111111X', '00000000', '614996097', '123456789'].each do |invalid_ico|
|
17
|
+
it "ICO is invalid with value #{invalid_ico.inspect}" do
|
18
|
+
expect(subject.valid_ico?(invalid_ico)).to be_falsey
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/ico_validator_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe IcoValidator do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
it
|
20
|
+
it 'ICO is valid with integer value in right format' do
|
21
21
|
subject.ico = 61499609
|
22
22
|
|
23
23
|
expect(subject).to be_valid
|
@@ -27,7 +27,7 @@ describe IcoValidator do
|
|
27
27
|
it "ICO is invalid with value #{invalid_ico.inspect}" do
|
28
28
|
subject.ico = invalid_ico
|
29
29
|
expect(subject).not_to be_valid
|
30
|
-
expect(subject.errors.messages).to eq({ico: [
|
30
|
+
expect(subject.errors.messages).to eq({ ico: ['Invalid ICO format.'] })
|
31
31
|
end
|
32
32
|
end
|
33
33
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ico-validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Premysl Donat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -100,9 +100,11 @@ files:
|
|
100
100
|
- ico-validator.gemspec
|
101
101
|
- lib/ico-validator.rb
|
102
102
|
- lib/ico-validator/engine.rb
|
103
|
+
- lib/ico-validator/ico_validation.rb
|
103
104
|
- lib/ico-validator/ico_validator.rb
|
104
105
|
- lib/locale/cs.yml
|
105
106
|
- lib/locale/en.yml
|
107
|
+
- spec/ico_validation_spec.rb
|
106
108
|
- spec/ico_validator_spec.rb
|
107
109
|
- spec/javascripts/support/jasmine.yml
|
108
110
|
- spec/javascripts/support/jasmine_helper.rb
|
@@ -128,13 +130,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
130
|
version: '0'
|
129
131
|
requirements: []
|
130
132
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.4.
|
133
|
+
rubygems_version: 2.4.8
|
132
134
|
signing_key:
|
133
135
|
specification_version: 4
|
134
136
|
summary: Rails validator for validating format of czech identification number = IC
|
135
137
|
test_files:
|
138
|
+
- spec/ico_validation_spec.rb
|
136
139
|
- spec/ico_validator_spec.rb
|
137
140
|
- spec/javascripts/support/jasmine.yml
|
138
141
|
- spec/javascripts/support/jasmine_helper.rb
|
139
142
|
- spec/javascripts/validator_spec.js
|
140
143
|
- spec/spec_helper.rb
|
144
|
+
has_rdoc:
|