run_cl 1.0.20 → 1.0.21
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.
- data/README.md +60 -2
- data/lib/run_cl.rb +27 -0
- data/lib/run_cl/run.rb +7 -5
- data/lib/run_cl/run_validator.rb +2 -2
- data/lib/run_cl/uniq_run_validator.rb +13 -0
- data/lib/run_cl/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# RunCl
|
2
2
|
|
3
|
-
|
3
|
+
Generator, Validator, Formatter/deformatter, for Chilean RUN (Rol Único Nacional)
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,65 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
At your model:
|
22
|
+
|
23
|
+
include RunCl::ActAsRun
|
24
|
+
has_run_cl :rut
|
25
|
+
|
26
|
+
Where 'rut' is the name of the column that stores the RUN.
|
27
|
+
|
28
|
+
If you are using factorygirl put this in your factory:
|
29
|
+
|
30
|
+
FactoryGirl.define do
|
31
|
+
factory :user do
|
32
|
+
rut Run.for(:user, :rut)
|
33
|
+
....
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Where 'rut' is the name of the column that stores the RUN.
|
38
|
+
|
39
|
+
|
40
|
+
If you want to check a rut:
|
41
|
+
|
42
|
+
Run.valid? '11.111.111-1'
|
43
|
+
====>> true
|
44
|
+
|
45
|
+
Run.valid? '100000000'
|
46
|
+
====>> false
|
47
|
+
|
48
|
+
Example of valid formats are:
|
49
|
+
|
50
|
+
11.111.111-1
|
51
|
+
11111111-1
|
52
|
+
111111111
|
53
|
+
|
54
|
+
13.601.658-k
|
55
|
+
13601658-k
|
56
|
+
13601658k
|
57
|
+
|
58
|
+
13.601.658-K
|
59
|
+
13601658-K
|
60
|
+
13601658K
|
61
|
+
|
62
|
+
If you need to generate a valid RUN:
|
63
|
+
|
64
|
+
Run.generate
|
65
|
+
|
66
|
+
|
67
|
+
If you need to format a RUN:
|
68
|
+
|
69
|
+
Run.format('111111111')
|
70
|
+
====>> 11.111.111-1
|
71
|
+
|
72
|
+
If you need to remove a RUN format:
|
73
|
+
|
74
|
+
Run.remove_format('11.111.111-1')
|
75
|
+
====>> 111111111
|
76
|
+
|
77
|
+
## Assumptions
|
78
|
+
|
79
|
+
You are saving the RUN as a text
|
22
80
|
|
23
81
|
## Contributing
|
24
82
|
|
data/lib/run_cl.rb
CHANGED
@@ -1,7 +1,34 @@
|
|
1
1
|
require 'run_cl/version'
|
2
2
|
require 'run_cl/run'
|
3
3
|
require 'run_cl/run_validator'
|
4
|
+
require 'run_cl/uniq_run_validator'
|
4
5
|
|
5
6
|
|
6
7
|
module RunCl
|
8
|
+
|
9
|
+
module ActAsRun
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
module InstanceMethods
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
include InstanceMethods
|
17
|
+
def has_run_cl(name, options = {})
|
18
|
+
if not options[:run].presence or options[:run]
|
19
|
+
validates name, run: true
|
20
|
+
end
|
21
|
+
|
22
|
+
if not options[:uniq_run].presence or options[:uniq_run]
|
23
|
+
validates name, uniq_run: true
|
24
|
+
end
|
25
|
+
|
26
|
+
before_save "make_#{name}_format!"
|
27
|
+
|
28
|
+
define_method "make_#{name}_format!" do
|
29
|
+
self.send("#{name}=", Rut.remove_format(self.send("#{name}")) )
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
7
34
|
end
|
data/lib/run_cl/run.rb
CHANGED
@@ -28,17 +28,19 @@ module Run
|
|
28
28
|
def self.for model, attribute, randomize=true
|
29
29
|
valid_rut = randomize ? self.generate : '111111111'
|
30
30
|
|
31
|
-
|
32
|
-
|
31
|
+
model_class = model.to_s.camelize.constantize
|
32
|
+
if model_class.respond_to?(:table_name) && ActiveRecord::Base.connection.table_exists?(model_class.table_name)
|
33
33
|
while model_class.where(attribute.to_sym => valid_rut).any? do valid_rut = self.generate end
|
34
|
+
else
|
35
|
+
puts "not ActiveRecord::Base.connection.table_exists? model.to_s"
|
34
36
|
end
|
35
37
|
|
36
38
|
valid_rut
|
37
39
|
end
|
38
40
|
|
39
41
|
# Generar un run valido segun reglas chilenas
|
40
|
-
def self.generate
|
41
|
-
run = (rand * 20000000).round + 5000000
|
42
|
+
def self.generate
|
43
|
+
run = (rand * 20000000).round + 5000000
|
42
44
|
|
43
45
|
suma = 0
|
44
46
|
mul = 2
|
@@ -62,7 +64,7 @@ module Run
|
|
62
64
|
end
|
63
65
|
|
64
66
|
# Revisa si el run entregado es valido, el run debe incluir el digito verificador
|
65
|
-
def
|
67
|
+
def valid? run
|
66
68
|
if run
|
67
69
|
run_copy = run.clone
|
68
70
|
run_copy = run_copy.match(/^(\d+)\.{1}\d{3}\.{1}\d{3}-{1}(\d|k|K)$|^(\d+)\d{6}\-{1}(\d|k|K)|^(\d+)\d{6}(\d|k|K)$/i).to_s
|
data/lib/run_cl/run_validator.rb
CHANGED
@@ -3,11 +3,11 @@
|
|
3
3
|
class RunValidator < ActiveModel::Validator
|
4
4
|
def validate record
|
5
5
|
options[:attributes].each do |attribute|
|
6
|
-
record.errors[attribute.to_sym] << 'no es válido' unless Run.
|
6
|
+
record.errors[attribute.to_sym] << 'no es válido!!!' unless Run.valid? record.send(attribute)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
10
|
private
|
11
|
-
# record.errors[attribute.to_sym] << I18n.t('run.invalid') unless Run.
|
11
|
+
# record.errors[attribute.to_sym] << I18n.t('run.invalid') unless Run.valid? record.send(attribute)
|
12
12
|
|
13
13
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class UniqRunValidator < ActiveModel::Validator
|
4
|
+
def validate record
|
5
|
+
options[:attributes].each do |attribute|
|
6
|
+
run = Run.remove_format record.send(attribute)
|
7
|
+
record.errors[attribute.to_sym] << 'ya está en uso!!!' if record.class.where(attribute => run).any?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
# record.errors[attribute.to_sym] << I18n.t('run.in_use') unless Run.valid? record.send(attribute)
|
13
|
+
end
|
data/lib/run_cl/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: run_cl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.21
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Formateador/Desformateador, Generador, Validador de Rut Chilenos
|
15
15
|
email:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- lib/run_cl.rb
|
27
27
|
- lib/run_cl/run.rb
|
28
28
|
- lib/run_cl/run_validator.rb
|
29
|
+
- lib/run_cl/uniq_run_validator.rb
|
29
30
|
- lib/run_cl/version.rb
|
30
31
|
- run_cl.gemspec
|
31
32
|
homepage: https://bitbucket.org/mespina/run
|