spanish_vat_validators 0.0.5 → 0.0.6

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: f49c9508eb9c4c688e43844e46dee9bad4fcbd60
4
- data.tar.gz: b521947d481687bcd9a867ce051d6028138cfdcc
3
+ metadata.gz: 042cc74c82fad9975ca931b60656399205481640
4
+ data.tar.gz: f10c5c00c7a24dfbe6d242233f5853a993caa570
5
5
  SHA512:
6
- metadata.gz: 66b4e2bdf012638b20fe30b0f8887572bd4b1cd19dde903d871e04c29e9b7c477f4ae275e493a3595af34263892994e94ab35009585d76d775993359f0d833d3
7
- data.tar.gz: daf2bbc008244189955988097f08bba03b9b85029091743a01e44bd3f0ecbf5affa4f280a5615812273d5de5c149db4a72d00381439af7701730a82450325dcc
6
+ metadata.gz: de01a00f17c2b6a170377962f9684526282ab56e70e80aba607a4d5b58ca3ac964924d3355e49f3fe49831ab99067be8179e07483510c42915b7f89208f89d0b
7
+ data.tar.gz: 6356d4cb13fbcd74cf0e44693238390a5b54c07d76ecbb982c623865824ab1377df9c1137131a008f5058ea2c800ea17f11332ca6c09c68982006da819966ab1
data/.travis.yml ADDED
@@ -0,0 +1,39 @@
1
+ sudo: false
2
+
3
+ cache: bundler
4
+
5
+ script: 'bundle exec rake'
6
+
7
+ rvm:
8
+ - 1.9.3
9
+ - 2.0.0
10
+ - 2.1.5
11
+ - 2.2.0
12
+ - 2.2.4
13
+ - 2.3.0
14
+
15
+ gemfile:
16
+ - gemfiles/rails_3.2.gemfile
17
+ - gemfiles/rails_4.0.gemfile
18
+ - gemfiles/rails_4.1.gemfile
19
+ - gemfiles/rails_4.2.gemfile
20
+ - gemfiles/rails_5.0.gemfile
21
+
22
+ matrix:
23
+ fast_finish: true
24
+ exclude:
25
+ - rvm: 1.9.3
26
+ gemfile: gemfiles/rails_5.0.gemfile
27
+ - rvm: 2.0.0
28
+ gemfile: gemfiles/rails_5.0.gemfile
29
+ - rvm: 2.1.5
30
+ gemfile: gemfiles/rails_5.0.gemfile
31
+ - rvm: 2.2.0
32
+ gemfile: gemfiles/rails_5.0.gemfile
33
+
34
+ addons:
35
+ code_climate:
36
+ repo_token: 3a0f3547e95bcf5828df4c53639a2267fe0d61d9ac816220772e5337120aceeb
37
+
38
+ after_success:
39
+ - bundle exec codeclimate-test-reporter
data/Appraisals ADDED
@@ -0,0 +1,19 @@
1
+ appraise "rails-3.2" do
2
+ gem 'activemodel', '~> 3.2.0'
3
+ end
4
+
5
+ appraise "rails-4.0" do
6
+ gem "activemodel", '~> 4.0.0'
7
+ end
8
+
9
+ appraise "rails-4.1" do
10
+ gem "activemodel", '~> 4.1.0'
11
+ end
12
+
13
+ appraise "rails-4.2" do
14
+ gem "activemodel", '~> 4.2.0'
15
+ end
16
+
17
+ appraise "rails-5.0" do
18
+ gem "activemodel", '~> 5.0.0'
19
+ end
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # SpanishVatValidators
1
+ # SpanishVatValidators [![Build Status](https://travis-ci.org/dgilperez/spanish_vat_validators.svg?branch=master)](https://travis-ci.org/dgilperez/spanish_vat_validators)[![Code Climate](https://codeclimate.com/github/dgilperez/spanish_vat_validators/badges/gpa.svg)](https://codeclimate.com/github/dgilperez/spanish_vat_validators)
2
2
 
3
- I've adapted the code from the ValidateSpanishVAT plugin from https://github.com/lleirborras/ValidateSpanishVAT to be distributed as a gem for ease of use with Rails 3.
3
+ I've adapted the code from the ValidateSpanishVAT plugin from https://github.com/lleirborras/ValidateSpanishVAT to be distributed as a gem for ease of use with Rails >= 3.2.
4
4
 
5
5
  Also added I18n support for error messages, which you can change by adding this to your locale yml:
6
6
 
@@ -56,6 +56,12 @@ Just use any of the following validators.
56
56
  validates :nif, :valid_spanish_vat => true
57
57
  end
58
58
 
59
+ You can override the error message using the `message` option.
60
+
61
+ class Person < ActiveRecord::Base
62
+ validates :dni, valid_nif: { message: 'invalid' }
63
+ end
64
+
59
65
  ## Contributing
60
66
 
61
67
  1. Fork it
data/Rakefile CHANGED
@@ -1,2 +1,11 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
7
+
8
+ desc "Open an irb session preloaded with this library"
9
+ task :console do
10
+ sh "irb -Ilib -rspanish_vat_validators"
11
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 3.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ spanish_vat_validators (0.0.5)
5
+ activemodel (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (3.2.22.4)
11
+ activesupport (= 3.2.22.4)
12
+ builder (~> 3.0.0)
13
+ activesupport (3.2.22.4)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ multi_json (~> 1.0)
16
+ appraisal (2.2.0)
17
+ bundler
18
+ rake
19
+ thor (>= 0.14.0)
20
+ builder (3.0.4)
21
+ codeclimate-test-reporter (1.0.8)
22
+ simplecov (<= 0.13)
23
+ diff-lcs (1.3)
24
+ docile (1.1.5)
25
+ i18n (0.7.0)
26
+ json (2.1.0)
27
+ multi_json (1.12.1)
28
+ rake (12.0.0)
29
+ rspec (3.5.0)
30
+ rspec-core (~> 3.5.0)
31
+ rspec-expectations (~> 3.5.0)
32
+ rspec-mocks (~> 3.5.0)
33
+ rspec-core (3.5.4)
34
+ rspec-support (~> 3.5.0)
35
+ rspec-expectations (3.5.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.5.0)
38
+ rspec-mocks (3.5.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-support (3.5.0)
42
+ simplecov (0.13.0)
43
+ docile (~> 1.1.0)
44
+ json (>= 1.8, < 3)
45
+ simplecov-html (~> 0.10.0)
46
+ simplecov-html (0.10.0)
47
+ thor (0.19.4)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ activemodel (~> 3.2.0)
54
+ appraisal
55
+ bundler (~> 1.6)
56
+ codeclimate-test-reporter (~> 1.0.0)
57
+ rake
58
+ rspec
59
+ simplecov
60
+ spanish_vat_validators!
61
+
62
+ BUNDLED WITH
63
+ 1.13.2
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 4.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ spanish_vat_validators (0.0.5)
5
+ activemodel (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.0.13)
11
+ activesupport (= 4.0.13)
12
+ builder (~> 3.1.0)
13
+ activesupport (4.0.13)
14
+ i18n (~> 0.6, >= 0.6.9)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ appraisal (2.2.0)
20
+ bundler
21
+ rake
22
+ thor (>= 0.14.0)
23
+ builder (3.1.4)
24
+ codeclimate-test-reporter (1.0.8)
25
+ simplecov (<= 0.13)
26
+ diff-lcs (1.3)
27
+ docile (1.1.5)
28
+ i18n (0.7.0)
29
+ json (2.1.0)
30
+ minitest (4.7.5)
31
+ multi_json (1.12.1)
32
+ rake (12.0.0)
33
+ rspec (3.5.0)
34
+ rspec-core (~> 3.5.0)
35
+ rspec-expectations (~> 3.5.0)
36
+ rspec-mocks (~> 3.5.0)
37
+ rspec-core (3.5.4)
38
+ rspec-support (~> 3.5.0)
39
+ rspec-expectations (3.5.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.5.0)
42
+ rspec-mocks (3.5.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.5.0)
45
+ rspec-support (3.5.0)
46
+ simplecov (0.13.0)
47
+ docile (~> 1.1.0)
48
+ json (>= 1.8, < 3)
49
+ simplecov-html (~> 0.10.0)
50
+ simplecov-html (0.10.0)
51
+ thor (0.19.4)
52
+ thread_safe (0.3.5)
53
+ tzinfo (0.3.51)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ activemodel (~> 4.0.0)
60
+ appraisal
61
+ bundler (~> 1.6)
62
+ codeclimate-test-reporter (~> 1.0.0)
63
+ rake
64
+ rspec
65
+ simplecov
66
+ spanish_vat_validators!
67
+
68
+ BUNDLED WITH
69
+ 1.13.2
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 4.1.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ spanish_vat_validators (0.0.5)
5
+ activemodel (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.1.16)
11
+ activesupport (= 4.1.16)
12
+ builder (~> 3.1)
13
+ activesupport (4.1.16)
14
+ i18n (~> 0.6, >= 0.6.9)
15
+ json (~> 1.7, >= 1.7.7)
16
+ minitest (~> 5.1)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 1.1)
19
+ appraisal (2.2.0)
20
+ bundler
21
+ rake
22
+ thor (>= 0.14.0)
23
+ builder (3.2.2)
24
+ codeclimate-test-reporter (1.0.8)
25
+ simplecov (<= 0.13)
26
+ diff-lcs (1.3)
27
+ docile (1.1.5)
28
+ i18n (0.7.0)
29
+ json (1.8.3)
30
+ minitest (5.9.1)
31
+ rake (12.0.0)
32
+ rspec (3.5.0)
33
+ rspec-core (~> 3.5.0)
34
+ rspec-expectations (~> 3.5.0)
35
+ rspec-mocks (~> 3.5.0)
36
+ rspec-core (3.5.4)
37
+ rspec-support (~> 3.5.0)
38
+ rspec-expectations (3.5.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-mocks (3.5.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.5.0)
44
+ rspec-support (3.5.0)
45
+ simplecov (0.13.0)
46
+ docile (~> 1.1.0)
47
+ json (>= 1.8, < 3)
48
+ simplecov-html (~> 0.10.0)
49
+ simplecov-html (0.10.0)
50
+ thor (0.19.4)
51
+ thread_safe (0.3.5)
52
+ tzinfo (1.2.2)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ activemodel (~> 4.1.0)
60
+ appraisal
61
+ bundler (~> 1.6)
62
+ codeclimate-test-reporter (~> 1.0.0)
63
+ rake
64
+ rspec
65
+ simplecov
66
+ spanish_vat_validators!
67
+
68
+ BUNDLED WITH
69
+ 1.13.2
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 4.2.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ spanish_vat_validators (0.0.5)
5
+ activemodel (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.7.1)
11
+ activesupport (= 4.2.7.1)
12
+ builder (~> 3.1)
13
+ activesupport (4.2.7.1)
14
+ i18n (~> 0.7)
15
+ json (~> 1.7, >= 1.7.7)
16
+ minitest (~> 5.1)
17
+ thread_safe (~> 0.3, >= 0.3.4)
18
+ tzinfo (~> 1.1)
19
+ appraisal (2.2.0)
20
+ bundler
21
+ rake
22
+ thor (>= 0.14.0)
23
+ builder (3.2.2)
24
+ codeclimate-test-reporter (1.0.8)
25
+ simplecov (<= 0.13)
26
+ diff-lcs (1.3)
27
+ docile (1.1.5)
28
+ i18n (0.7.0)
29
+ json (1.8.3)
30
+ minitest (5.9.1)
31
+ rake (12.0.0)
32
+ rspec (3.5.0)
33
+ rspec-core (~> 3.5.0)
34
+ rspec-expectations (~> 3.5.0)
35
+ rspec-mocks (~> 3.5.0)
36
+ rspec-core (3.5.4)
37
+ rspec-support (~> 3.5.0)
38
+ rspec-expectations (3.5.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-mocks (3.5.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.5.0)
44
+ rspec-support (3.5.0)
45
+ simplecov (0.13.0)
46
+ docile (~> 1.1.0)
47
+ json (>= 1.8, < 3)
48
+ simplecov-html (~> 0.10.0)
49
+ simplecov-html (0.10.0)
50
+ thor (0.19.4)
51
+ thread_safe (0.3.5)
52
+ tzinfo (1.2.2)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ activemodel (~> 4.2.0)
60
+ appraisal
61
+ bundler (~> 1.6)
62
+ codeclimate-test-reporter (~> 1.0.0)
63
+ rake
64
+ rspec
65
+ simplecov
66
+ spanish_vat_validators!
67
+
68
+ BUNDLED WITH
69
+ 1.13.2
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activemodel", "~> 5.0.0"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ spanish_vat_validators (0.0.5)
5
+ activemodel (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.0.0.1)
11
+ activesupport (= 5.0.0.1)
12
+ activesupport (5.0.0.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ appraisal (2.2.0)
18
+ bundler
19
+ rake
20
+ thor (>= 0.14.0)
21
+ codeclimate-test-reporter (1.0.8)
22
+ simplecov (<= 0.13)
23
+ concurrent-ruby (1.0.2)
24
+ diff-lcs (1.3)
25
+ docile (1.1.5)
26
+ i18n (0.7.0)
27
+ json (2.1.0)
28
+ minitest (5.9.1)
29
+ rake (12.0.0)
30
+ rspec (3.5.0)
31
+ rspec-core (~> 3.5.0)
32
+ rspec-expectations (~> 3.5.0)
33
+ rspec-mocks (~> 3.5.0)
34
+ rspec-core (3.5.4)
35
+ rspec-support (~> 3.5.0)
36
+ rspec-expectations (3.5.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.5.0)
39
+ rspec-mocks (3.5.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.5.0)
42
+ rspec-support (3.5.0)
43
+ simplecov (0.13.0)
44
+ docile (~> 1.1.0)
45
+ json (>= 1.8, < 3)
46
+ simplecov-html (~> 0.10.0)
47
+ simplecov-html (0.10.0)
48
+ thor (0.19.4)
49
+ thread_safe (0.3.5)
50
+ tzinfo (1.2.2)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activemodel (~> 5.0.0)
58
+ appraisal
59
+ bundler (~> 1.6)
60
+ codeclimate-test-reporter (~> 1.0.0)
61
+ rake
62
+ rspec
63
+ simplecov
64
+ spanish_vat_validators!
65
+
66
+ BUNDLED WITH
67
+ 1.13.2
@@ -1,3 +1,3 @@
1
1
  module SpanishVatValidators
2
- VERSION = "0.0.5"
2
+ VERSION = '0.0.6'
3
3
  end
@@ -1,11 +1,12 @@
1
- #encoding: utf-8
1
+ # encoding: utf-8
2
2
  require "spanish_vat_validators/version"
3
+ require 'active_model'
3
4
 
4
5
  module ActiveModel::Validations
5
6
 
6
7
  module SpanishVatValidatorsHelpers
7
- def message(kind='vat')
8
- I18n.translate!("errors.messages.not_valid_#{kind}") rescue 'is invalid'
8
+ def message(kind='spanish_vat')
9
+ options[:message] || (I18n.translate!("errors.messages.not_valid_#{kind}") rescue 'is invalid')
9
10
  end
10
11
 
11
12
  # Validates NIF
@@ -14,7 +15,7 @@ module ActiveModel::Validations
14
15
  value = v.upcase
15
16
  return false unless value.match(/^[0-9]{8}[a-z]$/i)
16
17
  letters = "TRWAGMYFPDXBNJZSQVHLCKE"
17
- check = value.slice!(value.length - 1..value.length - 1)
18
+ check = value.slice!(value.length - 1)
18
19
  calculated_letter = letters[value.to_i % 23].chr
19
20
  return check === calculated_letter
20
21
  end
@@ -24,38 +25,34 @@ module ActiveModel::Validations
24
25
  return false if v.nil? || v.empty?
25
26
  value = v.clone
26
27
  return false unless value.match(/^[a-wyz][0-9]{7}[0-9a-z]$/i)
27
- pares = 0
28
- impares = 0
29
- uletra = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"]
30
- texto = value.upcase
31
- regular = /^[ABCDEFGHKLMNPQRS]\d{7}[0-9,A-J]$/#g);
28
+ even = 0
29
+ odd = 0
30
+ uletter = ["J", "A", "B", "C", "D", "E", "F", "G", "H", "I"]
31
+ text = value.upcase
32
+ regular = /^[ABCDEFGHJKLMNPQRSVW]\d{7}[0-9,A-J]$/#g);
32
33
  if regular.match(value).blank?
33
34
  false
34
35
  else
35
- ultima = texto[8,1]
36
+ last = text[8,1]
36
37
 
37
38
  [1,3,5,7].collect do |cont|
38
- xxx = (2 * texto[cont,1].to_i).to_s + "0"
39
- impares += xxx[0,1].to_i + xxx[1,1].to_i
39
+ xxx = (2 * text[cont,1].to_i).to_s + "0"
40
+ odd += xxx[0,1].to_i + xxx[1,1].to_i
41
+ even += text[cont+1,1].to_i if cont < 7
40
42
  end
41
43
 
42
- [2,4,6].collect do |cont|
43
- pares += texto[cont,1].to_i
44
- end
45
-
46
- suma = (pares + impares).to_s
47
- unumero = suma.last.to_i
48
- unumero = (10 - unumero).to_s
49
- unumero = 0 if(unumero == 10)
50
-
51
- ((ultima == unumero) || (ultima == uletra[unumero.to_i]))
44
+ sum = (even + odd).to_s
45
+ unumber = sum[-1].to_i
46
+ unumber = (10 - unumber).to_s
47
+ unumber = 0 if(unumber.to_i == 10)
48
+ ((last.to_i == unumber.to_i) || (last == uletter[unumber.to_i]))
52
49
  end
53
50
  end
54
51
 
55
52
  # Validates NIE
56
53
  # a NIE is really a NIF with first number changed to capital X, Y or Z letter
57
54
  def validate_nie(v)
58
- return false if v.nil? || v.empty?
55
+ return false if v.nil? || v.empty?
59
56
  value = v.upcase
60
57
  return false unless value.match(/^[xyz][0-9]{7}[a-z]$/i)
61
58
  value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]]
@@ -67,7 +64,7 @@ module ActiveModel::Validations
67
64
  class ValidSpanishVatValidator < ActiveModel::EachValidator
68
65
  include SpanishVatValidatorsHelpers
69
66
  def validate_each(record, attribute, value)
70
- record.errors[attribute] = message unless validate_nif(value) or validate_cif(value) or validate_nie(value)
67
+ record.errors.add(attribute, message) unless validate_nif(value) or validate_cif(value) or validate_nie(value)
71
68
  end
72
69
  end
73
70
 
@@ -75,7 +72,7 @@ module ActiveModel::Validations
75
72
  class ValidSpanishIdValidator < ActiveModel::EachValidator
76
73
  include SpanishVatValidatorsHelpers
77
74
  def validate_each(record, attribute, value)
78
- record.errors[attribute] = message('spanish_id') unless validate_nif(value) or validate_nie(value)
75
+ record.errors.add(attribute, message('spanish_id')) unless validate_nif(value) or validate_nie(value)
79
76
  end
80
77
  end
81
78
 
@@ -83,7 +80,7 @@ module ActiveModel::Validations
83
80
  class ValidNifValidator < ActiveModel::EachValidator
84
81
  include SpanishVatValidatorsHelpers
85
82
  def validate_each(record, attribute,value)
86
- record.errors[attribute] = message('nif') unless validate_nif(value)
83
+ record.errors.add(attribute, message('nif')) unless validate_nif(value)
87
84
  end
88
85
  end
89
86
 
@@ -91,7 +88,7 @@ module ActiveModel::Validations
91
88
  class ValidCifValidator < ActiveModel::EachValidator
92
89
  include SpanishVatValidatorsHelpers
93
90
  def validate_each(record, attribute,value)
94
- record.errors[attribute] = message('cif') unless validate_cif(value)
91
+ record.errors.add(attribute, message('cif')) unless validate_cif(value)
95
92
  end
96
93
  end
97
94
 
@@ -99,7 +96,7 @@ module ActiveModel::Validations
99
96
  class ValidNieValidator < ActiveModel::EachValidator
100
97
  include SpanishVatValidatorsHelpers
101
98
  def validate_each(record, attribute,value)
102
- record.errors[attribute] = message('nie') unless validate_nie(value)
99
+ record.errors.add(attribute, message('nie')) unless validate_nie(value)
103
100
  end
104
101
  end
105
102
 
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.authors = ["Javier Toledo"]
6
6
  gem.email = ["javier@theagilemonkeys.com"]
7
7
  gem.description = %q{Provides validators for spanish VAT numbers (NIF, CIF and NIE)}
8
- gem.summary = %q{Provides Rails3 compatible validators for spanish VAT numbers (NIF, CIF and NIE), with support for I18n}
8
+ gem.summary = %q{Provides Rails3+ compatible validators for spanish VAT numbers (NIF, CIF and NIE), with support for I18n}
9
9
  gem.homepage = "https://github.com/agilemonkeys/spanish_vat_validators"
10
10
 
11
11
  gem.files = `git ls-files`.split($\)
@@ -14,4 +14,15 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "spanish_vat_validators"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = SpanishVatValidators::VERSION
17
+
18
+ gem.required_ruby_version = '>= 1.9.3'
19
+
20
+ gem.add_dependency 'activemodel', '>= 3.2.0'
21
+
22
+ gem.add_development_dependency 'bundler', '~> 1.6'
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'appraisal'
26
+ gem.add_development_dependency 'simplecov'
27
+ gem.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
17
28
  end
@@ -0,0 +1,155 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveModel::Validations::ValidSpanishVatValidator do
4
+ describe '#validate_each' do
5
+ it 'does not add errors with a valid number' do
6
+ %w[22472947S S6185663I Y8527549Z].each do |identification_number|
7
+ record = build_record(identification_number)
8
+ should_be_valid(record)
9
+ end
10
+ end
11
+
12
+ it 'adds errors with an invalid number' do
13
+ %w[Y8527549 S618-5663I 000 Y1527549Z invalid_zip].each do |identification_number|
14
+ record = build_record(identification_number)
15
+ should_be_invalid(record)
16
+ end
17
+ end
18
+
19
+ it 'adds errors with an invalid number and custom message' do
20
+ %w[Y8527549 S618-5663I 000 Y1527549Z invalid_zip].each do |identification_number|
21
+ record = build_record(identification_number)
22
+ should_be_invalid(record, 'invalid')
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ActiveModel::Validations::ValidSpanishIdValidator do
29
+ describe '#validate_each' do
30
+ it 'does not add errors with a valid number' do
31
+ %w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M
32
+ Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
33
+ record = build_record(identification_number)
34
+ should_be_valid(record)
35
+ end
36
+ end
37
+
38
+ it 'adds errors with an invalid number' do
39
+ %w[1234 96380632 284-59349T 5696134L S6185663I invalid_zip].each do |identification_number|
40
+ record = build_record(identification_number)
41
+ should_be_invalid(record)
42
+ end
43
+ end
44
+
45
+ it 'adds errors with an invalid number and custom message' do
46
+ %w[1234 96380632 284-59349T 5696134L S6185663I invalid_zip].each do |identification_number|
47
+ record = build_record(identification_number)
48
+ should_be_invalid(record, 'invalid')
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe ActiveModel::Validations::ValidNifValidator do
55
+ describe '#validate_each' do
56
+ it 'does not add errors with a valid number' do
57
+ %w[22472947S 96380632Y 28459349T 35696134L 86159868M 29052409M].each do |identification_number|
58
+ record = build_record(identification_number)
59
+ should_be_valid(record)
60
+ end
61
+ end
62
+
63
+ it 'adds errors with an invalid number' do
64
+ %w[1234 96380632 284-59349T 5696134L S6185663I Y8527549Z invalid_zip].each do |identification_number|
65
+ record = build_record(identification_number)
66
+ should_be_invalid(record)
67
+ end
68
+ end
69
+
70
+ it 'adds errors with an invalid number and custom message' do
71
+ %w[1234 96380632 284-59349T 5696134L S6185663I Y8527549Z invalid_zip].each do |identification_number|
72
+ record = build_record(identification_number)
73
+ should_be_invalid(record, 'invalid')
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ describe ActiveModel::Validations::ValidCifValidator do
80
+ describe '#validate_each' do
81
+ it 'does not add errors with a valid number' do
82
+ %w[S6185663I C2871341J G37880135 F43766880 A58818501 J27950005 V63423321].each do |identification_number|
83
+ record = build_record(identification_number)
84
+ should_be_valid(record)
85
+ end
86
+ end
87
+
88
+ it 'adds errors with an invalid number' do
89
+ %w[1234 2871341J 284-59349T 22472947S Y8527549Z invalid_zip].each do |identification_number|
90
+ record = build_record(identification_number)
91
+ should_be_invalid(record)
92
+ end
93
+ end
94
+
95
+ it 'adds errors with an invalid number and custom message' do
96
+ %w[1234 2871341J 284-59349T 22472947S Y8527549Z invalid_zip].each do |identification_number|
97
+ record = build_record(identification_number)
98
+ should_be_invalid(record, 'invalid')
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ describe ActiveModel::Validations::ValidNieValidator do
105
+ describe '#validate_each' do
106
+ it 'does not add errors with a valid number' do
107
+ %w[Y8527549Z Y8305424T X9393496C X0012309G Y9370869Q Y2995306F].each do |identification_number|
108
+ record = build_record(identification_number)
109
+ should_be_valid(record)
110
+ end
111
+ end
112
+
113
+ it 'adds errors with an invalid number' do
114
+ %w[8527549Z 22472947S 000 S6185663I invalid_zip].each do |identification_number|
115
+ record = build_record(identification_number)
116
+ should_be_invalid(record)
117
+ end
118
+ end
119
+
120
+ it 'adds errors with an invalid number and custom message' do
121
+ %w[8527549Z 22472947S 000 S6185663I invalid_zip].each do |identification_number|
122
+ record = build_record(identification_number)
123
+ should_be_invalid(record, 'invalid')
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ def should_be_valid(record)
130
+ described_class.new(attributes: :identification_number).validate(record)
131
+ expect(record.errors).to be_empty
132
+ end
133
+
134
+ def should_be_invalid(record, message = nil)
135
+ params = { attributes: :identification_number }
136
+ params[:message] = message if message
137
+ described_class.new(params).validate(record)
138
+ expect(record.errors.size).to eq 1
139
+ expect(record.errors.messages[:identification_number]).to include(message || 'is invalid')
140
+ end
141
+
142
+ def build_record(identification_number)
143
+ ValidationDummyClass.new.tap do |object|
144
+ object.identification_number = identification_number
145
+ end
146
+ end
147
+
148
+ class ValidationDummyClass
149
+ include ::ActiveModel::Validations
150
+ attr_accessor :identification_number
151
+
152
+ def self.name
153
+ 'TestClass'
154
+ end
155
+ end
@@ -0,0 +1,6 @@
1
+ if ENV['CI']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
6
+ require 'spanish_vat_validators'
metadata CHANGED
@@ -1,15 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanish_vat_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Toledo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-25 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-05-22 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: 3.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.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: appraisal
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
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.0
13
111
  description: Provides validators for spanish VAT numbers (NIF, CIF and NIE)
14
112
  email:
15
113
  - javier@theagilemonkeys.com
@@ -18,14 +116,28 @@ extensions: []
18
116
  extra_rdoc_files: []
19
117
  files:
20
118
  - ".gitignore"
119
+ - ".travis.yml"
120
+ - Appraisals
21
121
  - Gemfile
22
122
  - LICENSE
23
123
  - README.md
24
124
  - Rakefile
125
+ - gemfiles/rails_3.2.gemfile
126
+ - gemfiles/rails_3.2.gemfile.lock
127
+ - gemfiles/rails_4.0.gemfile
128
+ - gemfiles/rails_4.0.gemfile.lock
129
+ - gemfiles/rails_4.1.gemfile
130
+ - gemfiles/rails_4.1.gemfile.lock
131
+ - gemfiles/rails_4.2.gemfile
132
+ - gemfiles/rails_4.2.gemfile.lock
133
+ - gemfiles/rails_5.0.gemfile
134
+ - gemfiles/rails_5.0.gemfile.lock
25
135
  - init.rb
26
136
  - lib/spanish_vat_validators.rb
27
137
  - lib/spanish_vat_validators/version.rb
28
138
  - spanish_vat_validators.gemspec
139
+ - spec/spanish_vat_validators_spec.rb
140
+ - spec/spec_helper.rb
29
141
  homepage: https://github.com/agilemonkeys/spanish_vat_validators
30
142
  licenses: []
31
143
  metadata: {}
@@ -37,7 +149,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
149
  requirements:
38
150
  - - ">="
39
151
  - !ruby/object:Gem::Version
40
- version: '0'
152
+ version: 1.9.3
41
153
  required_rubygems_version: !ruby/object:Gem::Requirement
42
154
  requirements:
43
155
  - - ">="
@@ -45,9 +157,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
157
  version: '0'
46
158
  requirements: []
47
159
  rubyforge_project:
48
- rubygems_version: 2.6.2
160
+ rubygems_version: 2.6.11
49
161
  signing_key:
50
162
  specification_version: 4
51
- summary: Provides Rails3 compatible validators for spanish VAT numbers (NIF, CIF and
52
- NIE), with support for I18n
53
- test_files: []
163
+ summary: Provides Rails3+ compatible validators for spanish VAT numbers (NIF, CIF
164
+ and NIE), with support for I18n
165
+ test_files:
166
+ - spec/spanish_vat_validators_spec.rb
167
+ - spec/spec_helper.rb