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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f5b6f7916008ebce0e7709887fb2677eec84a15
4
- data.tar.gz: 888f0b734d86e4329e47dbc23c054176cf6954d5
3
+ metadata.gz: 36c0f4c596aaf1ecaeecad864c17577ee5bcb604
4
+ data.tar.gz: bc26e9492611e8185559900a8cc753ce560fc6a7
5
5
  SHA512:
6
- metadata.gz: 10227fc127a0b939b3215d5c69e37ff5afabf04755f564f476180d7e4dc6898566ee275e591baf5dbb66ceac0bf4aa68c8d51d969213e6b29c12f0938966781c
7
- data.tar.gz: be64e02531dfb3060c8e92b2f646d6108e0a2e93fdb46b43dc89e4cd5226da0e1a2af3388b9495d3df6d8472e25461cb502899f30d3b6e0ea210365ec419613a
6
+ metadata.gz: af15cfac13cbd03c1effaa3e57e78f8c8f8ba21aa444d0baf7ef54ae38188992f0aa6fd85740818f3bc26e0e5601a1686443af9507c9bf86ca8c335a293c6174
7
+ data.tar.gz: a69703830b510511847d69872394ad57fa70df20c02839238ce2f8702a8251b9ed96bf79f5207c23fd16c56b5ae5b058f73237dc7e20dee3234b755ed1e2bea0
@@ -1,3 +1,7 @@
1
+ ## v0.4.0
2
+
3
+ * Validator may be used without Rails
4
+
1
5
  ## v0.3.1
2
6
 
3
7
  * Fire 'change' event on paste ico from clipboard and validate
data/README.md CHANGED
@@ -38,6 +38,3 @@ application.js
38
38
  * ICO must contain only numbers
39
39
  * 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)
40
40
 
41
- ## TODO
42
-
43
- * Add JS validation for ICO fields
@@ -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.3.1'
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}
@@ -1,6 +1 @@
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
1
  require "ico-validator/ico_validator"
6
- require "ico-validator/engine"
@@ -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.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
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
@@ -17,7 +17,7 @@ describe IcoValidator do
17
17
  end
18
18
  end
19
19
 
20
- it "ICO is valid with integer value in right format" do
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: ["Invalid ICO format."]})
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.3.1
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-06-25 00:00:00.000000000 Z
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.6
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: