codice_fiscale 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@
3
3
  #
4
4
  # This file is part of codice_fiscale.
5
5
  #
6
- # codice_fiscale is free software: you can redistribute it and/or modify
6
+ # codice_fiscale is free software: you can redistribute it and/or modify
7
7
  # it under the terms of the GNU Affero General Public License as published
8
8
  # by the Free Software Foundation, either version 3 of the License, or
9
9
  # (at your option) any later version.
@@ -17,8 +17,8 @@
17
17
  # along with codice_fiscale. If not, see <http://www.gnu.org/licenses/>.
18
18
  #++
19
19
 
20
- class CodiceFiscale < String
21
- VERSION = '0.1'
20
+ class CodiceFiscale
21
+ VERSION = '0.2'
22
22
 
23
23
  MONTH = [nil, ?A, ?B, ?C, ?D, ?E, ?H, ?L, ?M, ?P, ?R, ?S, ?T]
24
24
 
@@ -82,14 +82,10 @@ class CodiceFiscale < String
82
82
 
83
83
  def name=(n)
84
84
  @name = n.upcase if n.is_a?(String) and n =~ /^[A-Z]+$/i
85
-
86
- generate_code!
87
85
  end
88
86
 
89
87
  def surname=(s)
90
88
  @surname = s.upcase if s.is_a?(String) and s =~ /^[A-Z]+$/i
91
-
92
- generate_code!
93
89
  end
94
90
 
95
91
  def bday=(day)
@@ -105,8 +101,6 @@ class CodiceFiscale < String
105
101
  @bday
106
102
  else @bday
107
103
  end
108
-
109
- generate_code!
110
104
  end
111
105
 
112
106
  def bplace=(place)
@@ -119,21 +113,17 @@ class CodiceFiscale < String
119
113
  prov: $2
120
114
  }
121
115
  end
122
-
123
- generate_code!
124
116
  end
125
117
 
126
118
  def gender=(gen)
127
- if gen == :M or gen == :m or gen =~ /^m(ale)?$/i
119
+ if gen.to_s =~ /^m(ale)?$/i
128
120
  @gender = :M
129
- elsif gen == :F or gen == :f or gen =~ /^f(emale)?$/i
121
+ elsif gen.to_s =~ /^f(emale)?$/i
130
122
  @gender = :F
131
123
  end
132
-
133
- generate_code!
134
124
  end
135
125
 
136
- def initialize(options = {})
126
+ def initialize(options={})
137
127
  self.name = options[:name]
138
128
  self.surname = options[:surname]
139
129
  self.bday = options[:bday]
@@ -141,59 +131,60 @@ class CodiceFiscale < String
141
131
  self.gender = options[:gender]
142
132
  end
143
133
 
144
- def generate_code!
134
+ def to_s(separator=nil)
145
135
  return unless self.data?
146
- code = ""
136
+ separator ||= ''
147
137
 
148
138
  # TRIPLET FOR SURNAME
149
- code << (self.surname.scan(/[^AEIOU]/).join + self.surname.scan(/[AEIOU]/).join)[0, 3].ljust(3, 'X')
139
+ surname = (self.surname.scan(/[^AEIOU]/).join + self.surname.scan(/[AEIOU]/).join)[0, 3].ljust(3, 'X')
150
140
 
151
141
  # TRIPLET FOR NAME
152
- cons = self.name.scan(/[^AEIOU]/).join
153
- cons << cons[1] && cons[1] = '' if cons.size > 3
154
- code << (cons + self.name.scan(/[AEIOU]/).join)[0, 3].ljust(3, 'X')
142
+ name = (self.name.scan(/[^AEIOU]/).join.tap {|cons|
143
+ cons << cons[1] && cons[1] = '' if cons.size > 3
144
+ } + self.name.scan(/[AEIOU]/).join)[0, 3].ljust(3, 'X')
155
145
 
156
146
  # QUINTET FOR BDAY AND GENDER
157
- code << self.bday[:year].to_s.rjust(2, '0')
158
- code << CodiceFiscale::MONTH[self.bday[:mon]]
159
- code << (self.bday[:day] + (self.gender == :F ? 40 : 0)).to_s.rjust(2, '0')
147
+ q = self.bday[:year].to_s.rjust(2, '0') +
148
+ CodiceFiscale::MONTH[self.bday[:mon]] +
149
+ (self.bday[:day] + (self.gender == :F ? 40 : 0)).to_s.rjust(2, '0')
160
150
 
161
151
  # QUARTET FOR BPLACE
162
- code << self.belfiore_code(self.bplace[:city], self.bplace[:prov])
163
-
152
+ b_place = self.belfiore_code(self.bplace[:city], self.bplace[:prov])
153
+
164
154
  # CONTROL CODE
165
- sum = 0
166
- code.split(//).each_with_index {|ch, i|
167
- sum += ALFANUM_TO_CODE[i.odd? ? :even : :odd][ch]
168
- }
169
- code << ((sum % 26) + 65).chr
155
+ code = ([surname, name, q, b_place].join.each_char.each_with_index.inject(0) {|sum, (ch, i)|
156
+ sum + ALFANUM_TO_CODE[i.odd? ? :even : :odd][ch]
157
+ } % 26 + 65).chr
170
158
 
171
- self.replace(code)
159
+ [surname, name, q, b_place, code].join(separator)
172
160
  end
173
161
 
174
- def belfiore_code(city, province = nil)
175
- code = nil
162
+ def self.belfiore_code(city, province=nil)
176
163
  File.open(File.join(DBDIR, (province and province == 'EE' ? 'ee.db' : "#{city[0].downcase}.db"))) {|f|
177
164
  f.each_line {|line|
178
165
  co, c, p = line.strip.split(';')
179
- code = co.dup and break if c == city and (province and province != 'EE' ? p == province : true)
166
+ break co if c == city and (province and province != 'EE' ? p == province : true)
180
167
  }
181
168
  }
169
+ end
182
170
 
183
- code
171
+ def belfiore_code(city, province=nil)
172
+ self.class.belfiore_code(city, province)
184
173
  end
185
174
 
186
- def alternative(level = 1)
187
- code = self.dup
188
- code.scan(/[0-9]/).reverse[0, level].each {|n|
189
- i = code.rindex(n)
190
- code[i] = ALTERNATIVE[code[i]]
175
+ def alternative(level=1, separator=nil)
176
+ self.to_s(separator).tap {|code|
177
+ code.scan(/[0-9]/).reverse[0, level].each {|n, i = code.rindex(n)|
178
+ code[i] = ALTERNATIVE[code[i]]
179
+ }
191
180
  }
192
-
193
- code
194
181
  end
195
182
 
196
- def alternative!(level = 1)
197
- self.replace(self.alternative(level))
183
+ def ==(code)
184
+ code = code.to_s.scan(/[A-Z0-9]/i).join
185
+ return true if self.to_s == code
186
+ (1..7).any? {|x|
187
+ self.alternative(x) == code
188
+ }
198
189
  end
199
190
  end
@@ -50,7 +50,7 @@ Z115;GRECIA
50
50
  Z402;GROENLANDIA
51
51
  Z706;GUAM
52
52
  Z509;GUATEMALA
53
- Z607;GUAYANA FRANCESE
53
+ Z607;GUIANA FRANCESE
54
54
  Z319;GUINEA
55
55
  Z321;GUINEA EQUATORIALE
56
56
  Z221;HONG KONG
File without changes
File without changes
File without changes
File without changes
metadata CHANGED
@@ -1,36 +1,30 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: codice_fiscale
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- version: "0.1"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
9
6
  platform: ruby
10
- authors:
7
+ authors:
11
8
  - shura
12
9
  autorequire:
13
10
  bindir: bin
14
11
  cert_chain: []
15
-
16
- date: 2011-01-15 00:00:00 +01:00
17
- default_executable:
12
+ date: 2011-11-15 00:00:00.000000000 Z
18
13
  dependencies: []
19
-
20
14
  description: Codice fiscale library
21
15
  email: shura1991@gmail.com
22
16
  executables: []
23
-
24
17
  extensions: []
25
-
26
18
  extra_rdoc_files: []
27
-
28
- files:
19
+ files:
20
+ - lib/db/y.db
21
+ - lib/db/k.db
29
22
  - lib/db/n.db
30
23
  - lib/db/s.db
31
24
  - lib/db/e.db
32
25
  - lib/db/b.db
33
26
  - lib/db/r.db
27
+ - lib/db/x.db
34
28
  - lib/db/l.db
35
29
  - lib/db/g.db
36
30
  - lib/db/a.db
@@ -41,6 +35,7 @@ files:
41
35
  - lib/db/f.db
42
36
  - lib/db/j.db
43
37
  - lib/db/t.db
38
+ - lib/db/w.db
44
39
  - lib/db/c.db
45
40
  - lib/db/i.db
46
41
  - lib/db/v.db
@@ -50,37 +45,28 @@ files:
50
45
  - lib/db/u.db
51
46
  - lib/db/o.db
52
47
  - lib/codice_fiscale.rb
53
- has_rdoc: true
54
48
  homepage: http://github.com/shurizzle/ruby-codice_fiscale
55
49
  licenses: []
56
-
57
50
  post_install_message:
58
51
  rdoc_options: []
59
-
60
- require_paths:
52
+ require_paths:
61
53
  - lib
62
- required_ruby_version: !ruby/object:Gem::Requirement
54
+ required_ruby_version: !ruby/object:Gem::Requirement
63
55
  none: false
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- segments:
68
- - 0
69
- version: "0"
70
- required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
61
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- segments:
76
- - 0
77
- version: "0"
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
78
66
  requirements: []
79
-
80
67
  rubyforge_project:
81
- rubygems_version: 1.3.7
68
+ rubygems_version: 1.8.11
82
69
  signing_key:
83
70
  specification_version: 3
84
- summary: Tiny library to make easy working with codice fiscale
71
+ summary: Tiny library to make easy working with italian "codice fiscale"
85
72
  test_files: []
86
-