rutcl 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +18 -6
- data/lib/rut.rb +27 -14
- data/test/unit/rut_formatting_test.rb +78 -36
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDEyZGRkOGI4ZTQ4MDJmY2I2YzZjNWU3NWUxMzU3YThjMDU4ZmMyYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjhiNGQ4MWYwMTBlMDMzNzgyMTM5YjlkYWMzYTUzZDA3NTM5OGY5OQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjNlYjJkNzhhYzMzZDYyN2Q5NTI4MTdmNWExZGMzMjRjNmE2OGE1M2Q4Zjdh
|
10
|
+
YWVlNTE4ODQ5MTAzMGFhOWQ1NGQ5ZDZjYjBkZWIzNDc0NDY4OTllODg4ZmFi
|
11
|
+
Nzc1NzFhMjk4NDMxOTdhOGQyOWVlODY3YTI5NTAyNDNlZGVjZDc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzcyNjBmZDgxZDg1NmQxOTVkNmIwZWFlMTEzMGMzY2U3YTRlNGIzYjRlM2Y5
|
14
|
+
MzhiZTgwODM5Y2RmZTE4ZmI4ZWI3Mjg1ZmRlYjBmMGNhZGFmMGI0NWUyMDJm
|
15
|
+
YzYwMWIyY2U3MzQ1MjI0ODIzNDE0MjRiNzUwOGQ3ODExYjAwMDY=
|
data/README.md
CHANGED
@@ -18,7 +18,8 @@ La gema habilita la clase Rut, la que cuenta con tres métodos:
|
|
18
18
|
|
19
19
|
- `valid?(rut)` retorna `true` si el rut es válido.
|
20
20
|
- `dv(rut)` retorna el dígito verificador del rut. El argumento debe ser un rut **sin dígito verificador**.
|
21
|
-
- `
|
21
|
+
- `format_rut_with_dv(rut, validar=true)` formatea el rut recibido como argumento. El rut debe incluir el dígito verificador.
|
22
|
+
- `format_rut_without_dv(rut)` formatea el rut recibido como argumento. El rut **NO** debe incluir el dígito verificador.
|
22
23
|
|
23
24
|
## Ejemplos
|
24
25
|
|
@@ -35,13 +36,24 @@ Rutcl::Rut.dv(1) #rut 1-9
|
|
35
36
|
Rutcl::Rut.dv(16365637) #rut 16.365.637-k
|
36
37
|
#=> "k"
|
37
38
|
|
38
|
-
# Formateo
|
39
|
-
Rutcl::Rut.
|
39
|
+
# Formateo para RUTs que incluyen dígito verificador
|
40
|
+
Rutcl::Rut.format_rut_with_dv("16365637k")
|
40
41
|
#=> "16.365.637-k"
|
41
|
-
Rutcl::Rut.
|
42
|
+
Rutcl::Rut.format_rut_with_dv("14569484-1")
|
42
43
|
#=> "14.569.484-1"
|
43
|
-
Rutcl::Rut.
|
44
|
+
Rutcl::Rut.format_rut_with_dv("14569484-0")
|
44
45
|
ArgumentError: "El rut 14569484-0 es inválido"
|
45
|
-
Rutcl::Rut.
|
46
|
+
Rutcl::Rut.format_rut_with_dv("14569484-0", false)
|
46
47
|
#=> "14.569.484-0"
|
48
|
+
|
49
|
+
# Formateo para RUTs que **NO** incluyen dígito verificador
|
50
|
+
Rutcl::Rut.format_rut_without_dv("16365637")
|
51
|
+
#=> "16.365.637-k"
|
52
|
+
Rutcl::Rut.format_rut_without_dv("14569484")
|
53
|
+
#=> "14.569.484-1"
|
47
54
|
```
|
55
|
+
|
56
|
+
## Changelog
|
57
|
+
|
58
|
+
### Versión 1.1.0 (2013-11-04)
|
59
|
+
- El método `pretty` es eliminado. En su lugar se crean dos métodos, `format_rut_without_dv(rut)` y `format_rut_with_dv(rut, validate=true)`.
|
data/lib/rut.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
module Rutcl
|
3
3
|
class Rut
|
4
4
|
|
5
|
-
VERSION = "1.0
|
5
|
+
VERSION = "1.1.0"
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def dv rut
|
@@ -21,22 +21,35 @@ module Rutcl
|
|
21
21
|
(r==10) ? "k" : r
|
22
22
|
end
|
23
23
|
|
24
|
-
def valid?
|
25
|
-
return true if
|
24
|
+
def valid? rut_with_dv
|
25
|
+
return true if format_rut_with_dv(rut_with_dv) rescue false
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
29
|
-
if
|
30
|
-
|
31
|
-
raise ArgumentError, "El rut está vacío" unless
|
32
|
-
raise ArgumentError, "El rut '#{
|
28
|
+
def format_rut_with_dv rut_with_dv, validate=true
|
29
|
+
if rut_with_dv.is_a? String
|
30
|
+
rut_with_dv.gsub! /[.\-\ ]/, ""
|
31
|
+
raise ArgumentError, "El rut está vacío" unless rut_with_dv.length > 0
|
32
|
+
raise ArgumentError, "El rut '#{rut_with_dv}' contiene caracteres inválidos" unless rut_with_dv =~ /^\d{1,8}[\dkK]$/
|
33
33
|
end
|
34
|
-
|
35
|
-
thisdv =
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
"#{
|
34
|
+
rut_with_dv = rut_with_dv.to_s.upcase
|
35
|
+
thisdv = rut_with_dv[-1, 1]
|
36
|
+
rut_without_dv = rut_with_dv[0, rut_with_dv.length - 1]
|
37
|
+
formatted_rut = format_rut_without_dv(rut_without_dv)
|
38
|
+
correct_dv = formatted_rut[-1, 1]
|
39
|
+
raise ArgumentError, "El rut #{rut_without_dv}-#{thisdv} es inválido" if validate && thisdv != correct_dv
|
40
|
+
formatted_rut
|
41
|
+
end
|
42
|
+
|
43
|
+
def format_rut_without_dv rut_without_dv
|
44
|
+
if rut_without_dv.is_a? String
|
45
|
+
rut_without_dv.gsub! /[.\ ]/, ""
|
46
|
+
raise ArgumentError, "El rut está vacío" unless rut_without_dv.length > 0
|
47
|
+
raise ArgumentError, "El rut '#{rut_without_dv}' contiene caracteres inválidos (use el método 'format_rut_with_dv' para RUTs que incluyan dígito veriicador)" unless rut_without_dv =~ /^\d{1,8}$/
|
48
|
+
end
|
49
|
+
rut_without_dv = rut_without_dv.to_s.upcase
|
50
|
+
dv = dv(rut_without_dv).to_s.upcase
|
51
|
+
rut_without_dv = rut_without_dv.reverse.gsub(/.{3}/, '\0.').gsub(/\.$/, '').reverse
|
52
|
+
"#{rut_without_dv}-#{dv}"
|
40
53
|
end
|
41
54
|
end
|
42
55
|
end
|
@@ -2,60 +2,102 @@
|
|
2
2
|
require 'test_helper'
|
3
3
|
|
4
4
|
describe Rutcl::Rut do
|
5
|
-
describe "
|
5
|
+
describe "complete RUT" do
|
6
6
|
describe "validations" do
|
7
7
|
it "should detect empty ruts" do
|
8
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
9
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
8
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "" }
|
9
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv nil }
|
10
10
|
end
|
11
11
|
it "should detect non-ruts" do
|
12
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
13
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
14
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
15
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
16
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
12
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "hola" }
|
13
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "14x569-484-1" }
|
14
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "14569a484-1" }
|
15
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "14569484/1" }
|
16
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "14 569x484 1" }
|
17
17
|
end
|
18
18
|
it "should detect invalid ruts" do
|
19
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
20
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
21
|
-
assert_raises(ArgumentError) { Rutcl::Rut.
|
19
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "1-8" }
|
20
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "100.001-1" }
|
21
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_with_dv "14.569.484-2" }
|
22
22
|
end
|
23
23
|
it "should dismiss invalid ruts if told so" do
|
24
|
-
Rutcl::Rut.
|
25
|
-
Rutcl::Rut.
|
26
|
-
Rutcl::Rut.
|
24
|
+
Rutcl::Rut.format_rut_with_dv("1-8", false).must_be_instance_of String
|
25
|
+
Rutcl::Rut.format_rut_with_dv("100.001-1", false).must_be_instance_of String
|
26
|
+
Rutcl::Rut.format_rut_with_dv("14.569.484-2", false).must_be_instance_of String
|
27
27
|
end
|
28
28
|
end
|
29
29
|
describe "formatting" do
|
30
30
|
it "should work with unformatted ruts" do
|
31
|
-
Rutcl::Rut.
|
32
|
-
Rutcl::Rut.
|
33
|
-
Rutcl::Rut.
|
34
|
-
Rutcl::Rut.
|
35
|
-
Rutcl::Rut.
|
36
|
-
Rutcl::Rut.
|
37
|
-
Rutcl::Rut.
|
38
|
-
Rutcl::Rut.
|
31
|
+
Rutcl::Rut.format_rut_with_dv(19).must_equal "1-9"
|
32
|
+
Rutcl::Rut.format_rut_with_dv(124).must_equal "12-4"
|
33
|
+
Rutcl::Rut.format_rut_with_dv("104k").must_equal "104-K"
|
34
|
+
Rutcl::Rut.format_rut_with_dv("10006").must_equal "1.000-6"
|
35
|
+
Rutcl::Rut.format_rut_with_dv("100005").must_equal "10.000-5"
|
36
|
+
Rutcl::Rut.format_rut_with_dv("1000012").must_equal "100.001-2"
|
37
|
+
Rutcl::Rut.format_rut_with_dv("79567728").must_equal "7.956.772-8"
|
38
|
+
Rutcl::Rut.format_rut_with_dv("145694841").must_equal "14.569.484-1"
|
39
39
|
end
|
40
40
|
it "should work with partially formatted ruts" do
|
41
|
-
Rutcl::Rut.
|
42
|
-
Rutcl::Rut.
|
43
|
-
Rutcl::Rut.
|
44
|
-
Rutcl::Rut.
|
45
|
-
Rutcl::Rut.
|
41
|
+
Rutcl::Rut.format_rut_with_dv("1000-6").must_equal "1.000-6"
|
42
|
+
Rutcl::Rut.format_rut_with_dv("10000-5").must_equal "10.000-5"
|
43
|
+
Rutcl::Rut.format_rut_with_dv("100001-2").must_equal "100.001-2"
|
44
|
+
Rutcl::Rut.format_rut_with_dv("7956772-8").must_equal "7.956.772-8"
|
45
|
+
Rutcl::Rut.format_rut_with_dv("14569484-1").must_equal "14.569.484-1"
|
46
46
|
end
|
47
47
|
it "should work with fully formatted ruts" do
|
48
|
-
Rutcl::Rut.
|
49
|
-
Rutcl::Rut.
|
50
|
-
Rutcl::Rut.
|
51
|
-
Rutcl::Rut.
|
52
|
-
Rutcl::Rut.
|
48
|
+
Rutcl::Rut.format_rut_with_dv("1.000-6").must_equal "1.000-6"
|
49
|
+
Rutcl::Rut.format_rut_with_dv("10.000-5").must_equal "10.000-5"
|
50
|
+
Rutcl::Rut.format_rut_with_dv("100.001-2").must_equal "100.001-2"
|
51
|
+
Rutcl::Rut.format_rut_with_dv("7.956.772-8").must_equal "7.956.772-8"
|
52
|
+
Rutcl::Rut.format_rut_with_dv("14.569.484-1").must_equal "14.569.484-1"
|
53
53
|
end
|
54
54
|
it "should work with ruts with spaces" do
|
55
|
-
Rutcl::Rut.
|
56
|
-
Rutcl::Rut.
|
57
|
-
Rutcl::Rut.
|
58
|
-
Rutcl::Rut.
|
55
|
+
Rutcl::Rut.format_rut_with_dv("1 9").must_equal "1-9"
|
56
|
+
Rutcl::Rut.format_rut_with_dv(" 12 4").must_equal "12-4"
|
57
|
+
Rutcl::Rut.format_rut_with_dv("104 k").must_equal "104-K"
|
58
|
+
Rutcl::Rut.format_rut_with_dv("100 0 6").must_equal "1.000-6"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
describe "incomplete RUT" do
|
63
|
+
describe "validations" do
|
64
|
+
it "should detect empty ruts" do
|
65
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "" }
|
66
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv nil }
|
67
|
+
end
|
68
|
+
it "should detect non-ruts" do
|
69
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "hola" }
|
70
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "14x569-484" }
|
71
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "14569a484" }
|
72
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "14569484/1" }
|
73
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "14 569x484 1" }
|
74
|
+
end
|
75
|
+
it "should reject already formatted ruts" do
|
76
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "15.776.844-1" }
|
77
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "14.569.484-1" }
|
78
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "12-4" }
|
79
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "104-K" }
|
80
|
+
assert_raises(ArgumentError) { Rutcl::Rut.format_rut_without_dv "104-k" }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
describe "formatting" do
|
84
|
+
it "should work with unformatted ruts" do
|
85
|
+
Rutcl::Rut.format_rut_without_dv("1").must_equal "1-9"
|
86
|
+
Rutcl::Rut.format_rut_without_dv("12").must_equal "12-4"
|
87
|
+
Rutcl::Rut.format_rut_without_dv("104").must_equal "104-K"
|
88
|
+
Rutcl::Rut.format_rut_without_dv("1000").must_equal "1.000-6"
|
89
|
+
Rutcl::Rut.format_rut_without_dv("10000").must_equal "10.000-5"
|
90
|
+
Rutcl::Rut.format_rut_without_dv("100001").must_equal "100.001-2"
|
91
|
+
Rutcl::Rut.format_rut_without_dv("7956772").must_equal "7.956.772-8"
|
92
|
+
Rutcl::Rut.format_rut_without_dv("14569484").must_equal "14.569.484-1"
|
93
|
+
Rutcl::Rut.format_rut_without_dv(1).must_equal "1-9"
|
94
|
+
Rutcl::Rut.format_rut_without_dv(12).must_equal "12-4"
|
95
|
+
Rutcl::Rut.format_rut_without_dv(104).must_equal "104-K"
|
96
|
+
Rutcl::Rut.format_rut_without_dv(1000).must_equal "1.000-6"
|
97
|
+
Rutcl::Rut.format_rut_without_dv(10000).must_equal "10.000-5"
|
98
|
+
Rutcl::Rut.format_rut_without_dv(100001).must_equal "100.001-2"
|
99
|
+
Rutcl::Rut.format_rut_without_dv(7956772).must_equal "7.956.772-8"
|
100
|
+
Rutcl::Rut.format_rut_without_dv(14569484).must_equal "14.569.484-1"
|
59
101
|
end
|
60
102
|
end
|
61
103
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rutcl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pablo Marambio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|