run_cl 1.0.14 → 1.0.15
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/run_cl/run_validator.rb +2 -0
- data/lib/run_cl/version.rb +1 -1
- data/lib/run_cl.rb +2 -91
- metadata +2 -2
data/lib/run_cl/run_validator.rb
CHANGED
data/lib/run_cl/version.rb
CHANGED
data/lib/run_cl.rb
CHANGED
@@ -1,96 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require 'run_cl/version'
|
2
|
+
require 'run_cl/run'
|
3
3
|
require 'run_cl/run_validator'
|
4
4
|
|
5
5
|
|
6
6
|
module RunCl
|
7
|
-
module Run
|
8
|
-
extend self
|
9
|
-
|
10
|
-
# Formatea un run
|
11
|
-
def self.format run
|
12
|
-
pos = 0
|
13
|
-
run.reverse
|
14
|
-
reverse = ""
|
15
|
-
run.reverse.gsub("-", "").gsub(".", "").split('').each_with_index do |c, index|
|
16
|
-
reverse += c
|
17
|
-
if index == 0
|
18
|
-
reverse += '-'
|
19
|
-
elsif (pos == 3)
|
20
|
-
reverse += "."
|
21
|
-
pos = 0
|
22
|
-
end
|
23
|
-
pos += 1
|
24
|
-
end
|
25
|
-
reverse.reverse
|
26
|
-
end
|
27
|
-
|
28
|
-
# Quita el formato de un run
|
29
|
-
def self.remove_format run
|
30
|
-
run.gsub("-", "").gsub(".", "")
|
31
|
-
end
|
32
|
-
|
33
|
-
# Generar un run valido segun reglas chilenas y que ademas es unico para el modelo entregado
|
34
|
-
def self.for model, attribute, randomize=true
|
35
|
-
valid_rut = randomize ? self.generate : '111111111'
|
36
|
-
|
37
|
-
if ActiveRecord::Base.connection.table_exists? model.to_s
|
38
|
-
model_class = model.to_s.camelize.constantize
|
39
|
-
while model_class.where(attribute.to_sym => valid_rut).any? do valid_rut = self.generate end
|
40
|
-
end
|
41
|
-
|
42
|
-
valid_rut
|
43
|
-
end
|
44
|
-
|
45
|
-
# Generar un run valido segun reglas chilenas
|
46
|
-
def self.generate seed = nil
|
47
|
-
run = (rand * 20000000).round + 5000000 unless seed.presence
|
48
|
-
|
49
|
-
suma = 0
|
50
|
-
mul = 2
|
51
|
-
run.to_s.reverse.split('').each do |i|
|
52
|
-
suma = suma + i.to_i * mul
|
53
|
-
if mul == 7
|
54
|
-
mul = 2
|
55
|
-
else
|
56
|
-
mul += 1
|
57
|
-
end
|
58
|
-
end
|
59
|
-
res = suma % 11
|
60
|
-
|
61
|
-
if res == 1
|
62
|
-
return "#{run}k"
|
63
|
-
elsif res == 0
|
64
|
-
return "#{run}0"
|
65
|
-
else
|
66
|
-
return "#{run}#{11-res}"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# Revisa si el run y digito entregado es valido
|
71
|
-
def self.check_run run, digit
|
72
|
-
return false if run.empty?
|
73
|
-
return false if digit.empty?
|
74
|
-
|
75
|
-
factor = 2
|
76
|
-
suma = 0
|
77
|
-
i = run.length - 1
|
78
|
-
|
79
|
-
while i >= 0
|
80
|
-
factor = 2 if factor > 7
|
81
|
-
suma = suma + ( run[i] ).to_i * factor.to_i
|
82
|
-
factor += 1
|
83
|
-
i -= 1
|
84
|
-
end
|
85
|
-
|
86
|
-
dv = 11 - ( suma % 11 )
|
87
|
-
dv = 0 if dv == 11
|
88
|
-
dv = "k" if dv == 10
|
89
|
-
|
90
|
-
return true if dv.to_s == digit.downcase
|
91
|
-
false
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
7
|
end
|