br_documents 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile.lock +10 -8
- data/lib/br_documents/ie_validator.rb +9 -8
- data/lib/br_documents/version.rb +1 -1
- data/spec/ie_validator_spec.rb +40 -30
- data/spec/spec_helper.rb +3 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 25cf99032b0bea6b2a016595a560e0a7ea5777154f74d9259009b50323473a61
|
4
|
+
data.tar.gz: f19aa466afa9077bfbe14184d8311bc7db6486d36d1f73ed855c2b00218a062a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f37a4a5244a984e8ed4bf93077e43dd92caec88ea2e9ee7243b8819c6c520050658d7fdcc3164518a7b324f0e777b78346cbe3e024bc019515e258d251d68a3
|
7
|
+
data.tar.gz: 67cf2292f3ca90a52ff6790155c6a32382f7c54475b7d572c3fd491f63a756676c88c333f83bd6f961cc97a6fc321df5139c2c7f10881a0af3239bafac313a5c
|
data/Gemfile.lock
CHANGED
@@ -1,30 +1,31 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
br_documents (0.1.
|
4
|
+
br_documents (0.1.3)
|
5
5
|
activemodel (>= 4.0.0)
|
6
6
|
i18n (>= 0.6.5)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activemodel (
|
12
|
-
activesupport (=
|
13
|
-
activesupport (
|
11
|
+
activemodel (6.0.0)
|
12
|
+
activesupport (= 6.0.0)
|
13
|
+
activesupport (6.0.0)
|
14
14
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
15
|
i18n (>= 0.7, < 2)
|
16
16
|
minitest (~> 5.1)
|
17
17
|
tzinfo (~> 1.1)
|
18
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
18
19
|
byebug (10.0.2)
|
19
20
|
codeclimate-test-reporter (1.0.7)
|
20
21
|
simplecov
|
21
22
|
concurrent-ruby (1.1.5)
|
22
23
|
diff-lcs (1.3)
|
23
24
|
docile (1.1.5)
|
24
|
-
i18n (1.
|
25
|
+
i18n (1.7.0)
|
25
26
|
concurrent-ruby (~> 1.0)
|
26
27
|
json (2.0.3)
|
27
|
-
minitest (5.
|
28
|
+
minitest (5.13.0)
|
28
29
|
rake (12.0.0)
|
29
30
|
rspec (3.5.0)
|
30
31
|
rspec-core (~> 3.5.0)
|
@@ -45,8 +46,9 @@ GEM
|
|
45
46
|
simplecov-html (~> 0.10.0)
|
46
47
|
simplecov-html (0.10.0)
|
47
48
|
thread_safe (0.3.6)
|
48
|
-
tzinfo (1.2.
|
49
|
+
tzinfo (1.2.6)
|
49
50
|
thread_safe (~> 0.1)
|
51
|
+
zeitwerk (2.2.2)
|
50
52
|
|
51
53
|
PLATFORMS
|
52
54
|
ruby
|
@@ -61,4 +63,4 @@ DEPENDENCIES
|
|
61
63
|
simplecov (>= 0.9.0)
|
62
64
|
|
63
65
|
BUNDLED WITH
|
64
|
-
1.
|
66
|
+
1.17.2
|
@@ -1,10 +1,9 @@
|
|
1
1
|
class IeValidator < ActiveModel::EachValidator
|
2
2
|
def validate_each(record, attribute, value)
|
3
|
-
|
4
|
-
attribute_uf_was_configured_at_validator?(record)
|
5
|
-
can_read_uf_at_record?(options, record)
|
3
|
+
ie_present?(value) &&
|
4
|
+
attribute_uf_was_configured_at_validator?(record) &&
|
5
|
+
can_read_uf_at_record?(options, record) &&
|
6
6
|
ie_valid?(record, attribute, value)
|
7
|
-
end
|
8
7
|
end
|
9
8
|
|
10
9
|
private
|
@@ -15,18 +14,20 @@ class IeValidator < ActiveModel::EachValidator
|
|
15
14
|
def attribute_uf_was_configured_at_validator?(record)
|
16
15
|
record.errors.add(:base,
|
17
16
|
I18n.t("validator.ie.uf.no_configured")) unless options[:uf].present?
|
18
|
-
|
17
|
+
|
18
|
+
options[:uf].present?
|
19
19
|
end
|
20
20
|
|
21
21
|
def can_read_uf_at_record?(options, record)
|
22
22
|
begin
|
23
|
-
read_uf(record)
|
23
|
+
uf = read_uf(record)
|
24
24
|
rescue NoMethodError
|
25
25
|
record.errors.add(:base, I18n.t("validator.ie.uf.no_present",
|
26
26
|
uf: options[:uf])
|
27
27
|
)
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
|
+
uf.present?
|
30
31
|
end
|
31
32
|
|
32
33
|
def ie_valid?(record, attribute, value)
|
@@ -42,7 +43,7 @@ class IeValidator < ActiveModel::EachValidator
|
|
42
43
|
uf = read_uf(record)
|
43
44
|
ie_number = BrDocuments::IE::Factory.create(uf, value)
|
44
45
|
ie_number.valid?
|
45
|
-
end
|
46
|
+
end
|
46
47
|
|
47
48
|
def read_uf(record)
|
48
49
|
attribute = record
|
data/lib/br_documents/version.rb
CHANGED
data/spec/ie_validator_spec.rb
CHANGED
@@ -1,73 +1,83 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
|
-
describe IeValidator do
|
4
|
-
let(:record) { double(
|
3
|
+
RSpec.describe IeValidator do
|
4
|
+
let(:record) { double('model') }
|
5
5
|
|
6
6
|
before do
|
7
|
-
allow(record).to receive(:uf) {
|
8
|
-
allow(record).to receive(:errors).and_return(
|
7
|
+
allow(record).to receive(:uf) {'SC'}
|
8
|
+
allow(record).to receive(:errors).and_return({})
|
9
9
|
allow(record.errors).to receive(:messages).and_return({})
|
10
10
|
allow(record.errors).to receive(:add) do | attribute, error |
|
11
|
-
record.errors
|
11
|
+
record.errors[attribute] ||= []
|
12
|
+
record.errors[attribute] << error
|
13
|
+
record.errors.messages[attribute] = record.errors[attribute]
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
subject { IeValidator.new(attributes:
|
17
|
+
subject { IeValidator.new(attributes: 'ie', uf: 'uf') }
|
16
18
|
|
17
19
|
context "when IE's number is valid" do
|
18
|
-
before { subject.validate_each(record,
|
20
|
+
before { subject.validate_each(record, 'ie', '253667852') }
|
19
21
|
|
20
|
-
it
|
22
|
+
it 'does not add errors in model' do
|
21
23
|
expect(record.errors.messages).to be_empty
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
|
-
context
|
26
|
-
before { subject.validate_each(record,
|
27
|
+
context 'when IE is blank' do
|
28
|
+
before { subject.validate_each(record, 'ie', '') }
|
27
29
|
|
28
|
-
it
|
30
|
+
it 'does not add errors in model' do
|
29
31
|
expect(record.errors.messages).to be_empty
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
|
-
context
|
34
|
-
|
35
|
+
context 'when IE is invalid' do
|
36
|
+
it 'adds :invalid error in the model' do
|
37
|
+
subject.validate_each(record, 'ie', '253667853')
|
35
38
|
|
36
|
-
|
37
|
-
|
39
|
+
expect(record.errors.messages['ie']).to include(:invalid)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'does not override the previous message' do
|
43
|
+
record.errors.add('ie', 'another error message')
|
44
|
+
subject.validate_each(record, 'ie', '253667853')
|
45
|
+
|
46
|
+
expect(record.errors.messages['ie']).to include('another error message')
|
47
|
+
expect(record.errors.messages['ie']).to include(:invalid)
|
38
48
|
end
|
39
49
|
end
|
40
50
|
|
41
|
-
context
|
51
|
+
context 'when UF is invalid' do
|
42
52
|
before do
|
43
|
-
allow(record).to receive(:uf){
|
44
|
-
subject.validate_each(record,
|
53
|
+
allow(record).to receive(:uf) {'XX'}
|
54
|
+
subject.validate_each(record, 'ie', '253667852')
|
45
55
|
end
|
46
56
|
|
47
|
-
it
|
48
|
-
expect(record.errors.messages[
|
57
|
+
it 'adds error in model' do
|
58
|
+
expect(record.errors.messages['ie']).to include t('validator.ie.uf.invalid')
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
52
|
-
context
|
62
|
+
context 'when cannot be found attribute uf in model' do
|
53
63
|
before do
|
54
64
|
allow(record).to receive(:uf).and_raise(NoMethodError)
|
55
|
-
subject.validate_each(record,
|
65
|
+
subject.validate_each(record, 'ie', '253667852')
|
56
66
|
end
|
57
67
|
|
58
|
-
it
|
68
|
+
it 'adds error in model' do
|
59
69
|
expect(record.errors.messages[:base]).to eql [
|
60
|
-
t(
|
70
|
+
t('validator.ie.uf.no_present', uf: 'uf')]
|
61
71
|
end
|
62
72
|
end
|
63
73
|
|
64
|
-
context
|
65
|
-
subject { IeValidator.new(attributes:
|
66
|
-
before { subject.validate_each(record,
|
74
|
+
context 'when attribute uf is not defined' do
|
75
|
+
subject { IeValidator.new(attributes: 'ie') }
|
76
|
+
before { subject.validate_each(record, 'ie', '253667852') }
|
67
77
|
|
68
|
-
it
|
78
|
+
it 'adds error in model' do
|
69
79
|
expect(record.errors.messages[:base]).to eql [
|
70
|
-
t(
|
80
|
+
t('validator.ie.uf.no_configured')]
|
71
81
|
end
|
72
82
|
end
|
73
83
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: br_documents
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ASSEINFO - Sistemas de Informação
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -239,8 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
239
239
|
- !ruby/object:Gem::Version
|
240
240
|
version: '0'
|
241
241
|
requirements: []
|
242
|
-
|
243
|
-
rubygems_version: 2.6.14.1
|
242
|
+
rubygems_version: 3.0.3
|
244
243
|
signing_key:
|
245
244
|
specification_version: 4
|
246
245
|
summary: Validates brazilian documents like CPF, CNPJ and IE. It can be used with
|