Class: FiscalCode

Inherits:
Object
  • Object
show all
Defined in:
lib/cf.rb

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.calc(name, surname, day, month, year, sex, city) ⇒ Object



157
158
159
160
# File 'lib/cf.rb', line 157

def self.calc(name, surname, day, month, year, sex, city)
  cf = FiscalCode.new(name, surname, day, month, year, sex, city)
  return cf.code
end

Instance Method Details

#codeObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cf.rb', line 123

def code
  @code = surname + 
    name + 
    birth_date + 
    city
  value = 0
  for i in 0..14 do
    c = @code[i,1]
    if (i % 2 != 0)
      if c =~ /[0-9]/
        value += c.to_i
      else
        value += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(c)
      end
    else
      if (c =~ /[0-9]/) 
        value += 1 if (c == '0')
        value += 5 if (c == '2')
        value += 7 if (c == '3')
        value += 9 if (c == '4')
        value += 13 if (c == '5')
        value += 15 if (c == '6')
        value += 17 if (c == '7')
        value += 19 if (c == '8')
        value += 21 if (c == '9')
      else 
        value += "BAKPLCQDREVOSFTGUHMINJWZYX".index(c);
      end
    end
  end
  value = value % 26;
  @code += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[value,1]
end