nifval 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,21 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nifval (0.1.4)
4
+ nifval (0.2.2)
5
5
  activemodel
6
6
  i18n
7
7
 
8
8
  GEM
9
9
  remote: http://rubygems.org/
10
10
  specs:
11
- activemodel (3.0.7)
12
- activesupport (= 3.0.7)
13
- builder (~> 2.1.2)
14
- i18n (~> 0.5.0)
15
- activesupport (3.0.7)
16
- builder (2.1.2)
11
+ activemodel (3.2.8)
12
+ activesupport (= 3.2.8)
13
+ builder (~> 3.0.0)
14
+ activesupport (3.2.8)
15
+ i18n (~> 0.6)
16
+ multi_json (~> 1.0)
17
+ builder (3.0.3)
17
18
  diff-lcs (1.1.2)
18
- i18n (0.5.0)
19
+ fuubar (1.0.0)
20
+ rspec (~> 2.0)
21
+ rspec-instafail (~> 0.2.0)
22
+ ruby-progressbar (~> 0.0.10)
23
+ i18n (0.6.1)
24
+ multi_json (1.3.6)
19
25
  rspec (2.5.0)
20
26
  rspec-core (~> 2.5.0)
21
27
  rspec-expectations (~> 2.5.0)
@@ -23,11 +29,14 @@ GEM
23
29
  rspec-core (2.5.1)
24
30
  rspec-expectations (2.5.0)
25
31
  diff-lcs (~> 1.1.2)
32
+ rspec-instafail (0.2.4)
26
33
  rspec-mocks (2.5.0)
34
+ ruby-progressbar (0.0.10)
27
35
 
28
36
  PLATFORMS
29
37
  ruby
30
38
 
31
39
  DEPENDENCIES
40
+ fuubar
32
41
  nifval!
33
42
  rspec (~> 2.5)
@@ -5,30 +5,86 @@ require "active_model"
5
5
  module Nifval
6
6
  class NifValidator < ActiveModel::EachValidator
7
7
  def validate_each(record, attribute, value)
8
- if !is_valid_nif value
8
+ if value.nil? || !Document.new(value).valid?
9
9
  record.errors.add(attribute, I18n.t("nifval.wrong"))
10
10
  end
11
11
  end
12
12
 
13
13
  private
14
14
 
15
- def is_valid_nif nif
16
- # NIF not provided
17
- return false if nif.nil?
15
+ class Document
16
+ attr_accessor :nif
17
+ def initialize(nif)
18
+ @nif = nif
18
19
 
19
- # Add zeros to the left if applyable, and accept lowercase
20
- nif = nif.rjust(9,'0')
21
- nif = nif.upcase
20
+ setup
21
+ end
22
+
23
+ def valid?
24
+ case
25
+ when invalid_format? then false
26
+ when standard? then valid_standard?
27
+ when special? then valid_special?
28
+ when cif? then valid_cif?
29
+ when nie? then valid_nie?
30
+ else false
31
+ end
32
+ end
22
33
 
23
- # Format
24
- return false if
34
+ def setup
35
+ raise MissingNifException if nif.nil?
36
+
37
+ # Add zeros to the left if applyable, and accept lowercase
38
+ @nif = nif.rjust(9,'0')
39
+ nif.upcase!
40
+ end
41
+ private :setup
42
+
43
+ def invalid_format?
25
44
  !nif.match(/^[A-Z]{1}\d{7}[A-Z0-9]{1}$/) && !nif.match(/^[0-9]{8}[A-Z]{1}$/)
45
+ end
46
+
47
+ def standard?
48
+ nif.match(/^[0-9]{8}[A-Z]{1}$/)
49
+ end
50
+
51
+ def special?
52
+ nif.match(/^[KLM]{1}/)
53
+ end
54
+
55
+ def cif?
56
+ nif.match(/^[ABCDEFGHJNPQRSUVW]{1}/)
57
+ end
58
+
59
+ def nie?
60
+ nif.match(/^[XYZ]{1}/)
61
+ end
26
62
 
27
- if nif.match(/^[0-9]{8}[A-Z]{1}$/)
28
- # Standard NIF
63
+ def valid_cif?
64
+ nstr = cif_algorithm_value.to_s
65
+ (ival(nif[8]) == (64+cif_algorithm_value).chr) || (nif[8] == nstr[nstr.length-1])
66
+ end
67
+
68
+ def valid_nie?
69
+ niff = nif.gsub("X","0").gsub("Y","1").gsub("Z","2")
70
+ nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[niff[0..7].to_i % 23]
71
+ end
72
+
73
+ def valid_special?
74
+ nif[8] == (64+cif_algorithm_value).chr
75
+ end
76
+
77
+ def valid_standard?
29
78
  nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[nif[0..7].to_i % 23]
30
- else
31
- # CIF algorithm
79
+ end
80
+
81
+ def cif_algorithm_value
82
+ @cif_algorithm_value ||= calculate_cif_algorithm_value
83
+ end
84
+ private :cif_algorithm_value
85
+
86
+ def calculate_cif_algorithm_value
87
+ # CIF algorithm
32
88
  sum = ival(nif[2]) + ival(nif[4]) + ival(nif[6])
33
89
  [1,3,5,7].each do |i|
34
90
  t = (2*(ival(nif[i]))).to_s
@@ -37,32 +93,22 @@ module Nifval
37
93
  sum += t1+t2
38
94
  end
39
95
  sumstr = sum.to_s
40
- n = 10 - ival(sumstr[sumstr.length-1])
41
-
42
- if nif.match(/^[KLM]{1}/)
43
- # Special NIFs (as CIFs)
44
- nif[8] == (64+n).chr
45
- elsif nif.match(/^[ABCDEFGHJNPQRSUVW]{1}/)
46
- # CIFs
47
- nstr = n.to_s
48
- (ival(nif[8]) == (64+n).chr) || (nif[8] == nstr[nstr.length-1])
49
- elsif nif.match(/^[XYZ]{1}/)
50
- # NIE
51
- niff = nif.gsub("X","0").gsub("Y","1").gsub("Z","2")
52
- nif[8] == "TRWAGMYFPDXBNJZSQVHLCKE"[niff[0..7].to_i % 23]
96
+
97
+ (10 - ival(sumstr[sumstr.length-1]))
98
+ end
99
+ private :calculate_cif_algorithm_value
100
+
101
+ def ival v
102
+ if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("1.9")
103
+ v.to_i
53
104
  else
54
- false
105
+ v-48
55
106
  end
56
107
  end
108
+ private :ival
57
109
  end
58
110
 
59
- def ival v
60
- if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("1.9")
61
- v.to_i
62
- else
63
- v-48
64
- end
65
- end
111
+
66
112
  end
67
113
  end
68
114
 
@@ -1,3 +1,3 @@
1
1
  module NifVal
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.require_paths = ["lib"]
21
21
 
22
22
  s.add_development_dependency "rspec", "~> 2.5"
23
+ s.add_development_dependency "fuubar"
23
24
  s.add_dependency "activemodel"
24
25
  s.add_dependency "i18n"
25
26
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  class Test
4
4
  include ActiveModel::Validations
5
5
 
6
- validates :testfield, :nif => true
6
+ validates :testfield, nif: true
7
7
 
8
8
  attr_accessor :testfield
9
9
 
@@ -10,6 +10,7 @@
10
10
  %td= nif.value
11
11
  %td= link_to 'Show', nif
12
12
  %td= link_to 'Edit', edit_nif_path(nif)
13
- %td= link_to 'Destroy', nif, :confirm => 'Are you sure?', :method => :delete
13
+ %td= button_to 'Destroy', nif, :confirm => 'Are you sure?', :method => :delete
14
14
  %br/
15
15
  = link_to 'New Nif', new_nif_path
16
+
metadata CHANGED
@@ -1,76 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: nifval
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Albert Bellonch
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-04-29 00:00:00 +02:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.5'
22
+ type: :development
23
23
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
- requirements:
26
+ requirements:
27
27
  - - ~>
28
- - !ruby/object:Gem::Version
29
- hash: 9
30
- segments:
31
- - 2
32
- - 5
33
- version: "2.5"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.5'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fuubar
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
34
38
  type: :development
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: activemodel
38
39
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: activemodel
48
+ requirement: !ruby/object:Gem::Requirement
40
49
  none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
48
54
  type: :runtime
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: i18n
52
55
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: i18n
64
+ requirement: !ruby/object:Gem::Requirement
54
65
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
62
70
  type: :runtime
63
- version_requirements: *id003
64
- description: Validates a Spanish NIF/CIF/NIE by verifying that the digit control corresponds to the number.
65
- email:
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Validates a Spanish NIF/CIF/NIE by verifying that the digit control corresponds
79
+ to the number.
80
+ email:
66
81
  - albert@itnig.net
67
82
  executables: []
68
-
69
83
  extensions: []
70
-
71
84
  extra_rdoc_files: []
72
-
73
- files:
85
+ files:
74
86
  - .rspec
75
87
  - Gemfile
76
88
  - Gemfile.lock
@@ -151,40 +163,30 @@ files:
151
163
  - test_app/test/unit/helpers/static_helper_test.rb
152
164
  - test_app/test/unit/nif_test.rb
153
165
  - test_app/vendor/plugins/.gitkeep
154
- has_rdoc: true
155
166
  homepage: https://github.com/albertbellonch/nifval
156
167
  licenses: []
157
-
158
168
  post_install_message:
159
169
  rdoc_options: []
160
-
161
- require_paths:
170
+ require_paths:
162
171
  - lib
163
- required_ruby_version: !ruby/object:Gem::Requirement
172
+ required_ruby_version: !ruby/object:Gem::Requirement
164
173
  none: false
165
- requirements:
166
- - - ">="
167
- - !ruby/object:Gem::Version
168
- hash: 3
169
- segments:
170
- - 0
171
- version: "0"
172
- required_rubygems_version: !ruby/object:Gem::Requirement
174
+ requirements:
175
+ - - ! '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
179
  none: false
174
- requirements:
175
- - - ">="
176
- - !ruby/object:Gem::Version
177
- hash: 3
178
- segments:
179
- - 0
180
- version: "0"
180
+ requirements:
181
+ - - ! '>='
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
181
184
  requirements: []
182
-
183
185
  rubyforge_project: nifval
184
- rubygems_version: 1.5.2
186
+ rubygems_version: 1.8.24
185
187
  signing_key:
186
188
  specification_version: 3
187
189
  summary: Validates a Spanish NIF/CIF/NIE
188
- test_files:
190
+ test_files:
189
191
  - spec/nifval_spec.rb
190
192
  - spec/spec_helper.rb