run_cl 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/run_cl/run.rb +87 -0
  2. data/lib/run_cl/version.rb +1 -1
  3. metadata +3 -2
data/lib/run_cl/run.rb ADDED
@@ -0,0 +1,87 @@
1
+ module Run
2
+ extend self
3
+
4
+ # Formatea un run
5
+ def self.format run
6
+ pos = 0
7
+ run.reverse
8
+ reverse = ""
9
+ run.reverse.gsub("-", "").gsub(".", "").split('').each_with_index do |c, index|
10
+ reverse += c
11
+ if index == 0
12
+ reverse += '-'
13
+ elsif (pos == 3)
14
+ reverse += "."
15
+ pos = 0
16
+ end
17
+ pos += 1
18
+ end
19
+ reverse.reverse
20
+ end
21
+
22
+ # Quita el formato de un run
23
+ def self.remove_format run
24
+ run.gsub("-", "").gsub(".", "")
25
+ end
26
+
27
+ # Generar un run valido segun reglas chilenas y que ademas es unico para el modelo entregado
28
+ def self.for model, attribute, randomize=true
29
+ valid_rut = randomize ? self.generate : '111111111'
30
+
31
+ if ActiveRecord::Base.connection.table_exists? model.to_s
32
+ model_class = model.to_s.camelize.constantize
33
+ while model_class.where(attribute.to_sym => valid_rut).any? do valid_rut = self.generate end
34
+ end
35
+
36
+ valid_rut
37
+ end
38
+
39
+ # Generar un run valido segun reglas chilenas
40
+ def self.generate seed = nil
41
+ run = (rand * 20000000).round + 5000000 unless seed.presence
42
+
43
+ suma = 0
44
+ mul = 2
45
+ run.to_s.reverse.split('').each do |i|
46
+ suma = suma + i.to_i * mul
47
+ if mul == 7
48
+ mul = 2
49
+ else
50
+ mul += 1
51
+ end
52
+ end
53
+ res = suma % 11
54
+
55
+ if res == 1
56
+ return "#{run}k"
57
+ elsif res == 0
58
+ return "#{run}0"
59
+ else
60
+ return "#{run}#{11-res}"
61
+ end
62
+ end
63
+
64
+ # Revisa si el run y digito entregado es valido
65
+ def self.check_run run, digit
66
+ return false if run.empty?
67
+ return false if digit.empty?
68
+
69
+ factor = 2
70
+ suma = 0
71
+ i = run.length - 1
72
+
73
+ while i >= 0
74
+ factor = 2 if factor > 7
75
+ suma = suma + ( run[i] ).to_i * factor.to_i
76
+ factor += 1
77
+ i -= 1
78
+ end
79
+
80
+ dv = 11 - ( suma % 11 )
81
+ dv = 0 if dv == 11
82
+ dv = "k" if dv == 10
83
+
84
+ return true if dv.to_s == digit.downcase
85
+ false
86
+ end
87
+ end
@@ -1,3 +1,3 @@
1
1
  module RunCl
2
- VERSION = "1.0.15"
2
+ VERSION = "1.0.16"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 15
9
- version: 1.0.15
8
+ - 16
9
+ version: 1.0.16
10
10
  platform: ruby
11
11
  authors:
12
12
  - mespina
@@ -59,6 +59,7 @@ files:
59
59
  - README.md
60
60
  - Rakefile
61
61
  - lib/run_cl.rb
62
+ - lib/run_cl/run.rb
62
63
  - lib/run_cl/run_validator.rb
63
64
  - lib/run_cl/version.rb
64
65
  - run_cl.gemspec