id_ecuador 0.0.1.alpha
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/.rspec +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +7 -0
- data/id_ecuador.gemspec +26 -0
- data/lib/id_ecuador.rb +8 -0
- data/lib/id_ecuador/id.rb +151 -0
- data/lib/id_ecuador/model_additions.rb +66 -0
- data/lib/id_ecuador/railtie.rb +9 -0
- data/lib/id_ecuador/version.rb +3 -0
- data/spec/id_ecuador/id_spec.rb +111 -0
- data/spec/id_ecuador/model_additions_spec.rb +132 -0
- data/spec/id_ecuador_spec.rb +6 -0
- data/spec/spec_helper.rb +3 -0
- metadata +131 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mario Andrés Correa
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# IdEcuador
|
2
|
+
[](https://travis-ci.org/macool/id_ecuador)
|
3
|
+
|
4
|
+
Gema para validar la cédula o ruc de Ecuador
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'id_ecuador'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install id_ecuador
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
cedula = IdEcuador::Id.new "1104680135"
|
24
|
+
cedula.valid? # => true
|
25
|
+
cedula.tipo_id # => "Cédula Persona natural"
|
26
|
+
cedula.codigo_provincia # => 11
|
27
|
+
|
28
|
+
cedula_invalida = IdEcuador::Id.new "1105680134"
|
29
|
+
cedula_invalida.errors # => ["ID inválida"]
|
30
|
+
```
|
31
|
+
|
32
|
+
No validar automáticamente:
|
33
|
+
```ruby
|
34
|
+
cedula = IdEcuador::Id.new "1104680135", auto_validate: false
|
35
|
+
cedula.validate!.valid?
|
36
|
+
```
|
37
|
+
|
38
|
+
## Rails
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class User < ActiveRecord::Base
|
42
|
+
validates_id :identificacion
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Con opciones:
|
47
|
+
```ruby
|
48
|
+
class User < ActiveRecord::Base
|
49
|
+
validates_id :identificacion, allow_blank: false, message: "Cédula inválida", only: [:cedula, :ruc]
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Ejemplo API Rails:
|
54
|
+
```ruby
|
55
|
+
user = User.new identificacion: "110468135001"
|
56
|
+
user.identificacion_id_validator.class # => IdEcuador::Id
|
57
|
+
user.identificacion_tipo_id # => "RUC Persona natural"
|
58
|
+
user.identificacion_codigo_provincia # => 11
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/id_ecuador.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'id_ecuador/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "id_ecuador"
|
8
|
+
spec.version = IdEcuador::VERSION
|
9
|
+
spec.authors = ["Mario Andrés Correa"]
|
10
|
+
spec.email = ["a@macool.me"]
|
11
|
+
spec.description = %q{Validate Ecuador's CI and RUC}
|
12
|
+
spec.summary = %q{Gem to validate Ecuador's CI and RUC}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "supermodel"
|
25
|
+
|
26
|
+
end
|
data/lib/id_ecuador.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module IdEcuador
|
4
|
+
class Id
|
5
|
+
|
6
|
+
attr_reader :errors, :tipo_id, :codigo_provincia, :tipo_id_sym
|
7
|
+
|
8
|
+
def initialize(id="", options={})
|
9
|
+
@id = id.to_s
|
10
|
+
@errors = []
|
11
|
+
|
12
|
+
defaults = {
|
13
|
+
auto_validate: true
|
14
|
+
}
|
15
|
+
@options = defaults.merge options
|
16
|
+
validate! if @options[:auto_validate]
|
17
|
+
end
|
18
|
+
def valid?
|
19
|
+
validate! unless already_validated
|
20
|
+
@errors.empty?
|
21
|
+
end
|
22
|
+
def validate!
|
23
|
+
@already_validated = true
|
24
|
+
validate_length and evaluate_province_code and evaluate_third_digit
|
25
|
+
self
|
26
|
+
end
|
27
|
+
def already_validated
|
28
|
+
!!@already_validated
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
def validate_length
|
33
|
+
if [10, 13].include?(@id.length)
|
34
|
+
true
|
35
|
+
else
|
36
|
+
@errors << "Longitud incorrecta"
|
37
|
+
false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
def evaluate_province_code
|
41
|
+
provinces = 22
|
42
|
+
code = @id.slice(0,2).to_i
|
43
|
+
if code < 1 or code > provinces
|
44
|
+
@errors << "Código de provincia incorrecto"
|
45
|
+
false
|
46
|
+
else
|
47
|
+
@codigo_provincia = code
|
48
|
+
true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
def evaluate_third_digit
|
52
|
+
@third_digit = @id[2].to_i
|
53
|
+
|
54
|
+
# to keep products in next method:
|
55
|
+
@products = []
|
56
|
+
|
57
|
+
if [7, 8].include?(@third_digit)
|
58
|
+
@errors << "Tercer dígito es inválido"
|
59
|
+
return false
|
60
|
+
elsif @third_digit < 6
|
61
|
+
if @id.length > 10
|
62
|
+
@tipo_id = "RUC Persona natural"
|
63
|
+
@tipo_id_sym = :ruc
|
64
|
+
else
|
65
|
+
@tipo_id = "Cédula Persona natural"
|
66
|
+
@tipo_id_sym = :cedula
|
67
|
+
end
|
68
|
+
validate_last_digits_for_persona_natural and evaluate_sums_for_persona_natural!
|
69
|
+
elsif @third_digit == 6
|
70
|
+
@tipo_id = "Sociedad pública"
|
71
|
+
@tipo_id_sym = :sociedad_publica
|
72
|
+
validate_last_digits_for_sociedad_publica and evaluate_sums_for_sociedad_publica!
|
73
|
+
elsif @third_digit == 9
|
74
|
+
@tipo_id = "Sociedad privada o extranjera"
|
75
|
+
@tipo_id_sym = :sociedad_privada
|
76
|
+
validate_last_digits_for_sociedad_privada and evaluate_sums_for_sociedad_privada!
|
77
|
+
end
|
78
|
+
return true
|
79
|
+
end
|
80
|
+
def validate_last_digits_for_persona_natural
|
81
|
+
if @id.length > 10 and @id.slice(10,3) != "001"
|
82
|
+
@errors << "RUC de persona natural debe terminar en 001"
|
83
|
+
false
|
84
|
+
else
|
85
|
+
true
|
86
|
+
end
|
87
|
+
end
|
88
|
+
def validate_last_digits_for_sociedad_publica
|
89
|
+
if @id.slice(9,4) != "0001"
|
90
|
+
@errors << "RUC de empresa de sector público debe terminar en 0001"
|
91
|
+
false
|
92
|
+
else
|
93
|
+
true
|
94
|
+
end
|
95
|
+
end
|
96
|
+
def validate_last_digits_for_sociedad_privada
|
97
|
+
if @id.slice(10,3) != "001"
|
98
|
+
@errors << "RUC de entidad privada o extranjera debe terminar en 001"
|
99
|
+
false
|
100
|
+
else
|
101
|
+
true
|
102
|
+
end
|
103
|
+
end
|
104
|
+
def evaluate_sums_for_persona_natural!
|
105
|
+
@modulus = 10
|
106
|
+
@verifier = @id.slice(9,1).to_i
|
107
|
+
p = 2
|
108
|
+
@id.slice(0,9).chars do |c|
|
109
|
+
product = c.to_i * p
|
110
|
+
if product >= 10 then product -= 9 end
|
111
|
+
@products << product
|
112
|
+
p = if p == 2 then 1 else 2 end
|
113
|
+
end
|
114
|
+
do_sums!
|
115
|
+
self
|
116
|
+
end
|
117
|
+
def evaluate_sums_for_sociedad_publica!
|
118
|
+
@modulus = 11
|
119
|
+
@verifier = @id.slice(8,1).to_i
|
120
|
+
multipliers = [3, 2, 7, 6, 5, 4, 3, 2]
|
121
|
+
(0..7).each do |i|
|
122
|
+
@products[i] = @id[i].to_i * multipliers[i]
|
123
|
+
end
|
124
|
+
@products[8] = 0
|
125
|
+
do_sums!
|
126
|
+
self
|
127
|
+
end
|
128
|
+
def evaluate_sums_for_sociedad_privada!
|
129
|
+
@modulus = 11
|
130
|
+
@verifier = @id.slice(9,1).to_i
|
131
|
+
multipliers = [4, 3, 2, 7, 6, 5, 4, 3, 2]
|
132
|
+
(0..8).each do |i|
|
133
|
+
@products[i] = @id[i].to_i * multipliers[i]
|
134
|
+
end
|
135
|
+
do_sums!
|
136
|
+
self
|
137
|
+
end
|
138
|
+
def do_sums!
|
139
|
+
sum = 0
|
140
|
+
@products.each do |product|
|
141
|
+
sum += product
|
142
|
+
end
|
143
|
+
res = sum % @modulus
|
144
|
+
digit = if res == 0 then 0 else @modulus - res end
|
145
|
+
unless digit == @verifier
|
146
|
+
@errors << "ID inválida"
|
147
|
+
end
|
148
|
+
self
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module IdEcuador
|
4
|
+
module ModelAdditions
|
5
|
+
def validates_id(attribute, options={})
|
6
|
+
# options for only: [:cedula, :ruc, :sociedad_publica, :sociedad_privada]
|
7
|
+
defaults = {
|
8
|
+
allow_blank: true,
|
9
|
+
message: nil,
|
10
|
+
only: []
|
11
|
+
}
|
12
|
+
options = defaults.merge options
|
13
|
+
|
14
|
+
# to array if not:
|
15
|
+
unless options[:only].class == Array
|
16
|
+
options[:only] = [options[:only]]
|
17
|
+
end
|
18
|
+
# poner métodos:
|
19
|
+
|
20
|
+
# getter del atributo que siempre retorna una instancia de IdEcuador::Id y se encarga de hacer cache basado en el valor del atributo
|
21
|
+
class_eval <<-EVAL
|
22
|
+
def #{attribute}_id_validator
|
23
|
+
if not @id_ecuador_validator or (send(:#{attribute}) != @id_ecuador_validator_last_id)
|
24
|
+
@id_ecuador_validator = IdEcuador::Id.new(send(:#{attribute}))
|
25
|
+
@id_ecuador_validator_last_id = send(:#{attribute})
|
26
|
+
end
|
27
|
+
@id_ecuador_validator
|
28
|
+
end
|
29
|
+
EVAL
|
30
|
+
|
31
|
+
# extensiones para .tipo_id y .codigo_provincia
|
32
|
+
class_eval <<-EVAL
|
33
|
+
def #{attribute}_tipo_id
|
34
|
+
#{attribute}_id_validator.tipo_id
|
35
|
+
end
|
36
|
+
def #{attribute}_codigo_provincia
|
37
|
+
#{attribute}_id_validator.codigo_provincia
|
38
|
+
end
|
39
|
+
EVAL
|
40
|
+
|
41
|
+
validate do
|
42
|
+
if not options[:allow_blank] and send(:"#{attribute}").blank?
|
43
|
+
errors.add attribute.to_sym, (options[:message] or "No puede quedar en blanco")
|
44
|
+
else
|
45
|
+
# normal validations:
|
46
|
+
if not send(:"#{attribute}").blank? and not send(:"#{attribute}_id_validator").valid?
|
47
|
+
if options[:message]
|
48
|
+
errors.add attribute.to_sym, options[:message]
|
49
|
+
else
|
50
|
+
send(:"#{attribute}_id_validator").errors.each do |error|
|
51
|
+
errors.add attribute.to_sym, error
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
# for options:
|
56
|
+
unless options[:only].empty?
|
57
|
+
unless options[:only].include?(send(:"#{attribute}_id_validator").tipo_id_sym)
|
58
|
+
errors.add attribute.to_sym, (options[:message] or "Tipo de identificación no permitido")
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe IdEcuador::Id do
|
5
|
+
|
6
|
+
describe "interfaz de la clase" do
|
7
|
+
it "debe llamar automáticamente al método validate!" do
|
8
|
+
id = IdEcuador::Id.new("1104680135")
|
9
|
+
id.send(:already_validated).should be_true
|
10
|
+
end
|
11
|
+
it "no debe llamar automáticamente al método validate!" do
|
12
|
+
IdEcuador::Id.any_instance.stub(:validate!)
|
13
|
+
id = IdEcuador::Id.new("1104680135", validate: false)
|
14
|
+
id.send(:already_validated).should be_false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "funcionalidad de la clase" do
|
19
|
+
describe "longitud" do
|
20
|
+
# longitud debería ser 10 para cédulas y 13 para RUC
|
21
|
+
before :each do
|
22
|
+
@error = "Longitud incorrecta"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "debería fallar con longitud menor a 10 caracteres" do
|
26
|
+
id = IdEcuador::Id.new "11"
|
27
|
+
id.errors.should include(@error)
|
28
|
+
end
|
29
|
+
it "no debería incluir problemas de longitud con 10 caracteres" do
|
30
|
+
id = IdEcuador::Id.new "1104680135"
|
31
|
+
id.errors.should_not include(@error)
|
32
|
+
end
|
33
|
+
it "no debería incluir problemas de longitud con 13 caracteres" do
|
34
|
+
id = IdEcuador::Id.new "1104680135001"
|
35
|
+
id.errors.should_not include(@error)
|
36
|
+
end
|
37
|
+
it "debería fallar con longitud mayor a 13 caracteres" do
|
38
|
+
id = IdEcuador::Id.new "12345678910111213"
|
39
|
+
id.errors.should include(@error)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "código de provincia" do
|
44
|
+
# Hay 22 provincias. Los dos primeros dígitos son el código de la provincia. Debe ser mayor a 0 y menor a 22
|
45
|
+
it "debería fallar con código de provincia 23" do
|
46
|
+
id = IdEcuador::Id.new "2304680135"
|
47
|
+
id.errors.should include("Código de provincia incorrecto")
|
48
|
+
end
|
49
|
+
it "debería decir que el código de la provincia es 11" do
|
50
|
+
id = IdEcuador::Id.new "1104680135"
|
51
|
+
id.codigo_provincia.should eq(11)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "tercer dígito" do
|
56
|
+
# el tercer dígito no puede ser el número 7 ni 8
|
57
|
+
before :each do
|
58
|
+
@error = "Tercer dígito es inválido"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "debería fallar con tercer dígito 7" do
|
62
|
+
id = IdEcuador::Id.new "1174680135"
|
63
|
+
id.errors.should include(@error)
|
64
|
+
end
|
65
|
+
it "debería fallar con tercer dígito 8" do
|
66
|
+
id = IdEcuador::Id.new "1184680135"
|
67
|
+
id.errors.should include(@error)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "tipo de identificación" do
|
72
|
+
# con tercer dígito:
|
73
|
+
# 9 -> sociedades privadas o extranjeras
|
74
|
+
# 6 -> sociedades públicas
|
75
|
+
# 0..5 -> personas naturales
|
76
|
+
it "debería decir que es una persona natural" do
|
77
|
+
id = IdEcuador::Id.new "1104680135"
|
78
|
+
id.tipo_id.should eq("Cédula Persona natural")
|
79
|
+
end
|
80
|
+
it "debería decir que es el RUC de una persona natural" do
|
81
|
+
id = IdEcuador::Id.new "1104680135001"
|
82
|
+
id.tipo_id.should eq("RUC Persona natural")
|
83
|
+
end
|
84
|
+
it "debería decir que es una sociedad pública" do
|
85
|
+
id = IdEcuador::Id.new "1164680130001"
|
86
|
+
id.tipo_id.should eq("Sociedad pública")
|
87
|
+
end
|
88
|
+
it "debería decir que es una sociedad privada o extranjera" do
|
89
|
+
id = IdEcuador::Id.new "1194680135001"
|
90
|
+
id.tipo_id.should eq("Sociedad privada o extranjera")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "probar con algunas cédulas" do
|
95
|
+
it "debería decir que esta cédula es válida" do
|
96
|
+
IdEcuador::Id.new("1104680135").valid?.should be_true
|
97
|
+
end
|
98
|
+
it "debería decir que esta cédula es inválida" do
|
99
|
+
IdEcuador::Id.new("1104680134").valid?.should be_false
|
100
|
+
end
|
101
|
+
it "debería decir que estas cédulas son válidas" do
|
102
|
+
["1104077209",
|
103
|
+
"1102077425",
|
104
|
+
"1102019351",
|
105
|
+
"1102778014"].each do |cedula|
|
106
|
+
IdEcuador::Id.new(cedula).valid?.should be_true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
# tests with Rails' models
|
5
|
+
|
6
|
+
describe IdEcuador::ModelAdditions do
|
7
|
+
|
8
|
+
describe "with no options" do
|
9
|
+
class User < SuperModel::Base
|
10
|
+
extend IdEcuador::ModelAdditions
|
11
|
+
validates_id :identificacion
|
12
|
+
end
|
13
|
+
|
14
|
+
it "no debería levantar un error si la identificacion del usuario no está puesta" do
|
15
|
+
user = User.new identificacion: nil
|
16
|
+
user.save.should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "valida el ID de un usuario antes de guardarlo a la base de datos" do
|
20
|
+
it "debería decir que es válido" do
|
21
|
+
User.new(identificacion: "1104680135").valid?.should be_true
|
22
|
+
end
|
23
|
+
it "debería decir que es inválido" do
|
24
|
+
user = User.new(identificacion: "1104680134")
|
25
|
+
user.save.should be_false
|
26
|
+
user.errors[:identificacion].should include("ID inválida")
|
27
|
+
end
|
28
|
+
it "debería decir que la longitud es inválida" do
|
29
|
+
user = User.new(identificacion: "12345")
|
30
|
+
user.save.should be_false
|
31
|
+
user.errors[:identificacion].should include("Longitud incorrecta")
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "expirar el cache de la variable @id_ecuador_validator_last_id" do
|
35
|
+
it "debería decir que la cédula es válida y, al cambiarla, decir que es inválida" do
|
36
|
+
user = User.new
|
37
|
+
user.identificacion = "1104680135"
|
38
|
+
user.valid?.should be_true
|
39
|
+
user.identificacion = "1104680134"
|
40
|
+
user.valid?.should be_false
|
41
|
+
end
|
42
|
+
it "debería decir que la cédula es inválida y, al cambiarla, decir que es válida" do
|
43
|
+
user = User.new
|
44
|
+
user.identificacion = "1104680134"
|
45
|
+
user.valid?.should be_false
|
46
|
+
user.identificacion = "1104680135"
|
47
|
+
user.valid?.should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "debe agregar métodos al atributo" do
|
54
|
+
it "debe agregar método `identificacion_tipo_id`" do
|
55
|
+
user = User.new(identificacion: "1104680135")
|
56
|
+
user.respond_to?(:identificacion_tipo_id).should be_true
|
57
|
+
user.identificacion_tipo_id.should eq("Cédula Persona natural")
|
58
|
+
end
|
59
|
+
it "debe agregar método `identificacion_codigo_provincia`" do
|
60
|
+
user = User.new(identificacion: "1104680135")
|
61
|
+
user.respond_to?(:identificacion_codigo_provincia).should be_true
|
62
|
+
user.identificacion_codigo_provincia.should eq(11)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "with options" do
|
68
|
+
describe "don't allow_blank" do
|
69
|
+
class UserWithOptionsAllowBlank < SuperModel::Base
|
70
|
+
extend IdEcuador::ModelAdditions
|
71
|
+
validates_id :identificacion, allow_blank: false
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should say record is invalid as identificacion is blank" do
|
75
|
+
user = UserWithOptionsAllowBlank.new identificacion: nil
|
76
|
+
user.valid?.should be_false
|
77
|
+
user.errors[:identificacion].should include("No puede quedar en blanco")
|
78
|
+
end
|
79
|
+
end
|
80
|
+
describe "specify message" do
|
81
|
+
class UserWithOptionsMessage < SuperModel::Base
|
82
|
+
extend IdEcuador::ModelAdditions
|
83
|
+
validates_id :identificacion, allow_blank: false, message: "Not valid!"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "debería mostrar 'Not valid!' como error con identificacion nil" do
|
87
|
+
user = UserWithOptionsMessage.new identificacion: nil
|
88
|
+
user.valid?.should be_false
|
89
|
+
user.errors[:identificacion].should eq(["Not valid!"])
|
90
|
+
end
|
91
|
+
it "debería mostrar 'Not valid!' como error con cédula" do
|
92
|
+
user = UserWithOptionsMessage.new identificacion: "1104680134"
|
93
|
+
user.valid?.should be_false
|
94
|
+
user.errors[:identificacion].should eq(["Not valid!"])
|
95
|
+
end
|
96
|
+
end
|
97
|
+
describe "specify only options" do
|
98
|
+
describe "con cédula" do
|
99
|
+
class UserWithOptionsOnlyCedula < SuperModel::Base
|
100
|
+
extend IdEcuador::ModelAdditions
|
101
|
+
validates_id :identificacion, only: :cedula
|
102
|
+
end
|
103
|
+
|
104
|
+
it "debería decir que es válido" do
|
105
|
+
user = UserWithOptionsOnlyCedula.new identificacion: "1104680135"
|
106
|
+
user.valid?.should be_true
|
107
|
+
end
|
108
|
+
it "debería decir que es inválido" do
|
109
|
+
user = UserWithOptionsOnlyCedula.new identificacion: "1104680135001"
|
110
|
+
user.valid?.should be_false
|
111
|
+
user.errors[:identificacion].should include("Tipo de identificación no permitido")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
describe "con RUC" do
|
115
|
+
class UserWithOptionsOnlyRUC < SuperModel::Base
|
116
|
+
extend IdEcuador::ModelAdditions
|
117
|
+
validates_id :identificacion, only: :ruc
|
118
|
+
end
|
119
|
+
it "debería decir que es válido" do
|
120
|
+
user = UserWithOptionsOnlyRUC.new identificacion: "1104680135001"
|
121
|
+
user.valid?.should be_true
|
122
|
+
end
|
123
|
+
it "debería decir que es inválido" do
|
124
|
+
user = UserWithOptionsOnlyRUC.new identificacion: "1104680135"
|
125
|
+
user.valid?.should be_false
|
126
|
+
user.errors[:identificacion].should include("Tipo de identificación no permitido")
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: id_ecuador
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mario Andrés Correa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: supermodel
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: Validate Ecuador's CI and RUC
|
79
|
+
email:
|
80
|
+
- a@macool.me
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .rspec
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- id_ecuador.gemspec
|
92
|
+
- lib/id_ecuador.rb
|
93
|
+
- lib/id_ecuador/id.rb
|
94
|
+
- lib/id_ecuador/model_additions.rb
|
95
|
+
- lib/id_ecuador/railtie.rb
|
96
|
+
- lib/id_ecuador/version.rb
|
97
|
+
- spec/id_ecuador/id_spec.rb
|
98
|
+
- spec/id_ecuador/model_additions_spec.rb
|
99
|
+
- spec/id_ecuador_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>'
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 1.3.1
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.24
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Gem to validate Ecuador's CI and RUC
|
126
|
+
test_files:
|
127
|
+
- spec/id_ecuador/id_spec.rb
|
128
|
+
- spec/id_ecuador/model_additions_spec.rb
|
129
|
+
- spec/id_ecuador_spec.rb
|
130
|
+
- spec/spec_helper.rb
|
131
|
+
has_rdoc:
|