spanish_vat_validators 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.
data/README.md CHANGED
@@ -31,7 +31,7 @@ Or install it yourself as:
31
31
  Just use any of the following validators.
32
32
 
33
33
  # A person id
34
- class Person < ActiveRecord::Base
34
+ class Spaniard < ActiveRecord::Base
35
35
  validates :dni, :valid_nif => true
36
36
  end
37
37
 
@@ -45,9 +45,14 @@ Just use any of the following validators.
45
45
  validates :nie, :valid_nie => true
46
46
  end
47
47
 
48
+ # Any person id
49
+ class Person < ActiveRecord::Base
50
+ validates :any_id, :valid_spanish_id => true
51
+ end
52
+
48
53
  # Any kind of id is valid
49
54
  class SpanishSubject < ActiveRecord::Base
50
- validates :id, :valid_spanish_vat => true
55
+ validates :nif, :valid_spanish_vat => true
51
56
  end
52
57
 
53
58
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module SpanishVatValidators
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -11,10 +11,10 @@ module ActiveModel::Validations
11
11
  # Validates NIF
12
12
  def validate_nif(v)
13
13
  return false if v.nil? || v.empty?
14
- value = v.clone
14
+ value = v.upcase
15
15
  return false unless value.match(/[0-9]{8}[a-z]/i)
16
16
  letters = "TRWAGMYFPDXBNJZSQVHLCKE"
17
- check = value.slice!(value.length - 1..value.length - 1).upcase
17
+ check = value.slice!(value.length - 1..value.length - 1)
18
18
  calculated_letter = letters[value.to_i % 23].chr
19
19
  return check === calculated_letter
20
20
  end
@@ -52,14 +52,13 @@ module ActiveModel::Validations
52
52
  end
53
53
  end
54
54
 
55
- # Validates NIE, in fact is a fake, a NIE is really a NIF with first number changed to capital 'X' letter, so we change the first X to a 0 and then try to
56
- # pass the nif validator
55
+ # Validates NIE
56
+ # a NIE is really a NIF with first number changed to capital X, Y or Z letter
57
57
  def validate_nie(v)
58
- return false if v.nil? || v.empty?
59
- value = v.clone
60
- return false unless value.match(/[x][0-9]{7,8}[a-z]/i)
61
- value[0] = '0'
62
- value.slice(0) if value.size > 9
58
+ return false if v.nil? || v.empty?
59
+ value = v.upcase
60
+ return false unless value.match(/[xyz][0-9]{7}[a-z]/i)
61
+ value[0] = {"X" => "0", "Y" => "1", "Z" => "2"}[value[0]]
63
62
  validate_nif(value)
64
63
  end
65
64
  end
@@ -72,6 +71,14 @@ module ActiveModel::Validations
72
71
  end
73
72
  end
74
73
 
74
+ # Validates any Spanish person number
75
+ class ValidSpanishIdValidator < ActiveModel::EachValidator
76
+ include SpanishVatValidatorsHelpers
77
+ def validate_each(record, attribute, value)
78
+ record.errors[attribute] = message unless validate_nif(value) or validate_nie(value)
79
+ end
80
+ end
81
+
75
82
  # Validates NIF number only
76
83
  class ValidNifValidator < ActiveModel::EachValidator
77
84
  include SpanishVatValidatorsHelpers
@@ -92,7 +99,7 @@ module ActiveModel::Validations
92
99
  class ValidNieValidator < ActiveModel::EachValidator
93
100
  include SpanishVatValidatorsHelpers
94
101
  def validate_each(record, attribute,value)
95
- record.errors[attribute] = message('nie') unless validate_cif(value)
102
+ record.errors[attribute] = message('nie') unless validate_nie(value)
96
103
  end
97
104
  end
98
105
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanish_vat_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2013-04-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides validators for spanish VAT numbers (NIF, CIF and NIE)
15
15
  email:
@@ -53,4 +53,3 @@ specification_version: 3
53
53
  summary: Provides Rails3 compatible validators for spanish VAT numbers (NIF, CIF and
54
54
  NIE), with support for I18n
55
55
  test_files: []
56
- has_rdoc: