validates_russian 0.0.2 → 0.0.3

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: 1860cf574419dcd8f498496d2e585088d8c1273b
4
- data.tar.gz: 319956b9e6499c6c1bd9af910b410ff3fc2303c4
3
+ metadata.gz: ab2b00dcc3e4d96d2aa480758a7b855f78b2bf4d
4
+ data.tar.gz: 94936f7e5ee9d384638e7fa1816df07302346cf1
5
5
  SHA512:
6
- metadata.gz: 3018567ac467d977e7a5bb562bc67ff7e56c113c9eb7e6a865a7fbd883147757d9d48bc6118ebebd4f24e8e275fc5a8aa6376ea39d2e605f58b0c7f3bd375a03
7
- data.tar.gz: 0bfa685c2f6353248ea295867824a904ac2315cd781f64a76834f838f3f9ab051cb1bb06a791af285ee3e0d3ed14aac1049303c1df67bf2661bc3ba05d98398e
6
+ metadata.gz: 81e4d3fd1e5ab4c44fd1a1d3bf31eb9bd591fc265b1c5902a50f37a1518f2b63a2f76321bd7b836e46f681dea27cdb6a220fe65e50f00d7a34a5ff4fc49bd941
7
+ data.tar.gz: 8cf34eff30779459c739445e6dddc1fe9ef8e7835bd8dcf864f688f50047bc812410c1ebefb90ca7e831f8ee81d3aa848bf26fff02f98c5c21ce86e53ccbd9e2
data/README.md CHANGED
@@ -4,10 +4,11 @@
4
4
  [[инструкции разработчикам](https://github.com/asiniy/validates_russian/wiki/Инструкции-разработчикам)]
5
5
  [![Gem Version](https://badge.fury.io/rb/validates_russian.png)](http://badge.fury.io/rb/validates_russian)
6
6
  [![Build Status](https://travis-ci.org/asiniy/validates_russian.png?branch=master)](https://travis-ci.org/asiniy/validates_russian)
7
+ [![Code Climate](https://codeclimate.com/github/asiniy/validates_russian.png)](https://codeclimate.com/github/asiniy/validates_russian)
7
8
 
8
9
  Валидация русских значений:
9
10
 
10
- * ИНН (TODO)
11
+ * [ИНН](http://ru.wikipedia.org/wiki/Идентификационный_номер_налогоплательщика)
11
12
  * ОКПО (TODO)
12
13
  * ОКАТО (TODO)
13
14
  * [КПП](http://ru.wikipedia.org/wiki/Код_причины_постановки_на_учёт)
@@ -3,10 +3,11 @@
3
3
  [[по-русски](README.md)]
4
4
  [![Gem Version](https://badge.fury.io/rb/validates_russian.png)](http://badge.fury.io/rb/validates_russian)
5
5
  [![Build Status](https://travis-ci.org/asiniy/validates_russian.png?branch=master)](https://travis-ci.org/asiniy/validates_russian)
6
+ [![Code Climate](https://codeclimate.com/github/asiniy/validates_russian.png)](https://codeclimate.com/github/asiniy/validates_russian)
6
7
 
7
8
  Russian specific values validation:
8
9
 
9
- * INN (TODO)
10
+ * [INN](http://ru.wikipedia.org/wiki/Идентификационный_номер_налогоплательщика) (Russian analogue of VAT identification number)
10
11
  * OKPO (TODO)
11
12
  * OKATO (TODO)
12
13
  * [KPP](http://ru.wikipedia.org/wiki/Код_причины_постановки_на_учёт) (Code of reason for registration)
@@ -1,3 +1,3 @@
1
1
  module ValidatesRussian
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -0,0 +1,31 @@
1
+ class InnValidator < ValidatesRussian::Validator
2
+ validates_using do |inn|
3
+ next false unless ValidatesRussian::REGION_NUMBERS.include?(inn[0..1])
4
+ next false unless inn =~ /^\d+$/
5
+ next false if inn.size != 10 && inn.size != 12
6
+
7
+ inn = inn.split(//).map(&:to_i)
8
+
9
+ if inn.size == 10
10
+ n10 = calc(P10, inn)
11
+ next false unless n10 == inn[9]
12
+ end
13
+
14
+ if inn.size == 12
15
+ n11 = calc(P11, inn)
16
+ n12 = calc(P12, inn)
17
+ next false unless n11 == inn[10] && n12 == inn[11]
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def self.calc(p, inn)
24
+ p.each_with_index.inject(0){ |s, p| s + p[0] * inn[p[1]] } % 11 % 10
25
+ end
26
+
27
+ P10 = [2, 4, 10, 3, 5, 9, 4, 6, 8]
28
+ P11 = [7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
29
+ P12 = [3, 7, 2, 4, 10, 3, 5, 9, 4, 6, 8]
30
+ private_constant :P10, :P11, :P12
31
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe InnValidator do
4
+ before(:each) do
5
+ TestModel.reset_callbacks(:validate)
6
+ TestModel.validates(:field, inn: true)
7
+ end
8
+
9
+ it 'should be valid for valid values' do
10
+ valid_inns = %w{
11
+ 183501166447
12
+ 341800950695
13
+ 470313747100
14
+ 471900124013
15
+ 503102384852
16
+ 7706633181
17
+ 7715805253
18
+ 7714698320
19
+ 7830002293
20
+ 5036032527
21
+ }
22
+
23
+ valid_inns.each do |inn|
24
+ model = TestModel.new
25
+ model.field = inn
26
+ model.should be_valid
27
+ end
28
+ end
29
+
30
+ it 'should not be valid for invalid values' do
31
+ invalid_inns = %w(
32
+ #ffff
33
+ orange-duck
34
+ eee
35
+ xxx
36
+ epics
37
+ 1234567890
38
+ 123456789101
39
+ ).push('', ' ', nil)
40
+
41
+ invalid_inns.each do |inn|
42
+ model = TestModel.new
43
+ model.field = inn
44
+ model.should_not be_valid
45
+ end
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validates_russian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Antonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-18 00:00:00.000000000 Z
11
+ date: 2014-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -86,9 +86,11 @@ files:
86
86
  - lib/validates_russian/region_numbers.rb
87
87
  - lib/validates_russian/validator.rb
88
88
  - lib/validates_russian/version.rb
89
+ - lib/validators/inn_validator.rb
89
90
  - lib/validators/kpp_validator.rb
90
91
  - spec/spec_helper.rb
91
92
  - spec/support/test_model.rb
93
+ - spec/validators/inn_validator_spec.rb
92
94
  - spec/validators/kpp_validator_spec.rb
93
95
  - validates_russian.gemspec
94
96
  homepage: http://github.com/asiniy/validates_russian
@@ -118,4 +120,5 @@ summary: validates specific russian values
118
120
  test_files:
119
121
  - spec/spec_helper.rb
120
122
  - spec/support/test_model.rb
123
+ - spec/validators/inn_validator_spec.rb
121
124
  - spec/validators/kpp_validator_spec.rb