nifval 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module NifVal
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/nifval.rb CHANGED
@@ -1,12 +1,12 @@
1
+ # encoding: utf-8
1
2
  # Adapted from http://compartecodigo.com/javascript/validar-nif-cif-nie-segun-ley-vigente-31.html
2
-
3
3
  require "active_model"
4
4
 
5
5
  module NifVal
6
6
  class NifValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
8
  if !is_valid_nif value
9
- record.errors[attribute] << "NIF/CIF/NIE no válido"
9
+ record.errors.add(attribute, "NIF/NIE Inválido")
10
10
  end
11
11
  end
12
12
 
@@ -25,15 +25,15 @@ module NifVal
25
25
  nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[nif[0..7].to_i % 23]
26
26
  else
27
27
  # CIF algorithm
28
- sum = nif[2]-48 + nif[4]-48 + nif[6]-48
28
+ sum = ival(nif[2]) + ival(nif[4]) + ival(nif[6])
29
29
  [1,3,5,7].each do |i|
30
- t = (2*(nif[i]-48)).to_s
31
- t1 = t[0]-48
32
- t2 = t[1].nil? ? 0 : t[1]-48
30
+ t = (2*(ival(nif[i]))).to_s
31
+ t1 = ival(t[0])
32
+ t2 = t[1].nil? ? 0 : ival(t[1])
33
33
  sum += t1+t2
34
34
  end
35
35
  sumstr = sum.to_s
36
- n = 10 - (sumstr[sumstr.length-1]-48)
36
+ n = 10 - ival(sumstr[sumstr.length-1])
37
37
 
38
38
  if nif.match(/^[KLM]{1}/)
39
39
  # Special NIFs (as CIFs)
@@ -41,7 +41,7 @@ module NifVal
41
41
  elsif nif.match(/^[ABCDEFGHJNPQRSUVW]{1}/)
42
42
  # CIFs
43
43
  nstr = n.to_s
44
- (nif[8] == (64+n).chr) || (nif[8] == nstr[nstr.length-1])
44
+ (ival(nif[8]) == (64+n).chr) || (nif[8] == nstr[nstr.length-1])
45
45
  elsif nif.match(/^[XYZ]{1}/)
46
46
  # NIE
47
47
  niff = nif.gsub("X","0").gsub("Y","1").gsub("Z","2")
@@ -51,6 +51,10 @@ module NifVal
51
51
  end
52
52
  end
53
53
  end
54
+
55
+ def ival v
56
+ RUBY_VERSION<="1.8.7" ? (v - 48) : v.to_i
57
+ end
54
58
  end
55
59
  end
56
60
 
data/spec/nifval_spec.rb CHANGED
@@ -28,7 +28,7 @@ describe NifVal do
28
28
  # Invalid NIFs
29
29
  context "when we check invalid NIFs" do
30
30
  it "should return ERROR" do
31
- nif_validity "A2345678C", false
31
+ nif_validity "12345678T", false
32
32
  end
33
33
  end
34
34
 
@@ -42,7 +42,7 @@ describe NifVal do
42
42
  # Invalid CIFs
43
43
  context "when we check invalid CIFs" do
44
44
  it "should return ERROR" do
45
- nif_validity "12345678T", false
45
+ nif_validity "A2345678C", false
46
46
  end
47
47
  end
48
48
 
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifval
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
5
+ version: 0.1.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - Albert Bellonch
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-04-07 00:00:00 +02:00
13
+ date: 2011-04-14 00:00:00 +02:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
@@ -26,10 +21,6 @@ dependencies:
26
21
  requirements:
27
22
  - - ~>
28
23
  - !ruby/object:Gem::Version
29
- hash: 9
30
- segments:
31
- - 2
32
- - 5
33
24
  version: "2.5"
34
25
  type: :development
35
26
  version_requirements: *id001
@@ -41,9 +32,6 @@ dependencies:
41
32
  requirements:
42
33
  - - ">="
43
34
  - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
35
  version: "0"
48
36
  type: :runtime
49
37
  version_requirements: *id002
@@ -81,23 +69,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
69
  requirements:
82
70
  - - ">="
83
71
  - !ruby/object:Gem::Version
84
- hash: 3
85
- segments:
86
- - 0
87
72
  version: "0"
88
73
  required_rubygems_version: !ruby/object:Gem::Requirement
89
74
  none: false
90
75
  requirements:
91
76
  - - ">="
92
77
  - !ruby/object:Gem::Version
93
- hash: 3
94
- segments:
95
- - 0
96
78
  version: "0"
97
79
  requirements: []
98
80
 
99
81
  rubyforge_project: nifval
100
- rubygems_version: 1.5.2
82
+ rubygems_version: 1.6.1
101
83
  signing_key:
102
84
  specification_version: 3
103
85
  summary: Validates a Spanish NIF/CIF/NIE