spanish_vat_validators 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -30,25 +30,25 @@ Or install it yourself as:
30
30
 
31
31
  Just use any of the following validators.
32
32
 
33
- # A person id
34
- class Person < ActiveRecord::Base
35
- validates :dni, :valid_nif => true
36
- end
37
-
38
- # A company id
39
- class Company < ActiveRecord::Base
40
- validates :cif, :valid_cif => true
41
- end
42
-
43
- # A foreigner id
44
- class Alien < ActiveRecord::Base
45
- validates :nie, :valid_nie => true
46
- end
47
-
48
- # Any kind of id is valid
49
- class SpanishSubject < ActiveRecord::Base
50
- validates :id, :valid_spanish_vat => true
51
- end
33
+ # A person id
34
+ class Person < ActiveRecord::Base
35
+ validates :dni, :valid_nif => true
36
+ end
37
+
38
+ # A company id
39
+ class Company < ActiveRecord::Base
40
+ validates :cif, :valid_cif => true
41
+ end
42
+
43
+ # A foreigner id
44
+ class Alien < ActiveRecord::Base
45
+ validates :nie, :valid_nie => true
46
+ end
47
+
48
+ # Any kind of id is valid
49
+ class SpanishSubject < ActiveRecord::Base
50
+ validates :id, :valid_spanish_vat => true
51
+ end
52
52
 
53
53
  ## Contributing
54
54
 
@@ -1,3 +1,4 @@
1
+ #encoding: utf-8
1
2
  require "spanish_vat_validators/version"
2
3
 
3
4
  module ActiveModel::Validations
@@ -8,7 +9,9 @@ module ActiveModel::Validations
8
9
  end
9
10
 
10
11
  # Validates NIF
11
- def validate_nif(value)
12
+ def validate_nif(v)
13
+ return false if v.nil? || v.empty?
14
+ value = v.clone
12
15
  return false unless value.match(/[0-9]{8}[a-z]/i)
13
16
  letters = "TRWAGMYFPDXBNJZSQVHLCKE"
14
17
  check = value.slice!(value.length - 1..value.length - 1).upcase
@@ -17,7 +20,9 @@ module ActiveModel::Validations
17
20
  end
18
21
 
19
22
  # Validates CIF
20
- def validate_cif(value)
23
+ def validate_cif(v)
24
+ return false if v.nil? || v.empty?
25
+ value = v.clone
21
26
  return false unless value.match(/[a-wyz][0-9]{7}[0-9a-z]/i)
22
27
  pares = 0
23
28
  impares = 0
@@ -49,7 +54,9 @@ module ActiveModel::Validations
49
54
 
50
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
51
56
  # pass the nif validator
52
- def validate_nie(value)
57
+ def validate_nie(v)
58
+ return false if v.nil? || v.empty?
59
+ value = v.clone
53
60
  return false unless value.match(/[x][0-9]{7,8}[a-z]/i)
54
61
  value[0] = '0'
55
62
  value.slice(0) if value.size > 9
@@ -61,7 +68,7 @@ module ActiveModel::Validations
61
68
  class ValidSpanishVatValidator < ActiveModel::EachValidator
62
69
  include SpanishVatValidatorsHelpers
63
70
  def validate_each(record, attribute, value)
64
- record.errors[attribute] = message unless validate_nif(value.clone) or validate_cif(value.clone) or validate_nie(value.clone)
71
+ record.errors[attribute] = message unless validate_nif(value) or validate_cif(value) or validate_nie(value)
65
72
  end
66
73
  end
67
74
 
@@ -69,7 +76,7 @@ module ActiveModel::Validations
69
76
  class ValidNifValidator < ActiveModel::EachValidator
70
77
  include SpanishVatValidatorsHelpers
71
78
  def validate_each(record, attribute,value)
72
- record.errors[attribute] = message('nif') unless validate_nif(value.clone)
79
+ record.errors[attribute] = message('nif') unless validate_nif(value)
73
80
  end
74
81
  end
75
82
 
@@ -77,7 +84,7 @@ module ActiveModel::Validations
77
84
  class ValidCifValidator < ActiveModel::EachValidator
78
85
  include SpanishVatValidatorsHelpers
79
86
  def validate_each(record, attribute,value)
80
- record.errors[attribute] = message('cif') unless validate_cif(value.clone)
87
+ record.errors[attribute] = message('cif') unless validate_cif(value)
81
88
  end
82
89
  end
83
90
 
@@ -85,7 +92,7 @@ module ActiveModel::Validations
85
92
  class ValidNieValidator < ActiveModel::EachValidator
86
93
  include SpanishVatValidatorsHelpers
87
94
  def validate_each(record, attribute,value)
88
- record.errors[attribute] = message('nie') unless validate_cif(value.clone)
95
+ record.errors[attribute] = message('nie') unless validate_cif(value)
89
96
  end
90
97
  end
91
98
 
@@ -1,3 +1,3 @@
1
1
  module SpanishVatValidators
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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.1
4
+ version: 0.0.2
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-06-22 00:00:00.000000000 Z
12
+ date: 2012-08-25 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provides validators for spanish VAT numbers (NIF, CIF and NIE)
15
15
  email: