rest_my_case 1.9.2 → 1.9.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 088ea74126da74d24cd42b6ca1bd23838cf16f74
4
- data.tar.gz: a5c583155c12293f7e23e23213bab13c74cf3544
3
+ metadata.gz: f7c0fba5355ebbec27dbca5c5d364b5541383361
4
+ data.tar.gz: 2f2a0d395b3371b7228c1c6973925fe5cab20163
5
5
  SHA512:
6
- metadata.gz: d48c0464b9ea66217c168963ebbef24542273a55336486e0c0dcbfb0a7bf5ae8bbdf38cbdfb7d4dbf646f401a7d9ff62196e9885c13493a1e8af19c418c39057
7
- data.tar.gz: 6c8546d1508b909c58e19287d7e8e8dc826f311a4e52387d7aeaf49b72d693e3d40816fba6da3c90d9253d4d3f5cdfed6fed21dc88c450cc613b6ffb5f73a69e
6
+ metadata.gz: 3414f0946225813a7632cdaf0dcfd2791e4a766adc34a93fddf8e2a9758a885ba0285e5fcd1bdd7c53c5f8ca47b3da7a57aad76cbc8eac74b48e0b3671299a78
7
+ data.tar.gz: 065550d6c4ec94909b57caa65f27470b5a43e5fb2393f8ba4f7672ab8572ec4004daa5caba3845f85495ac405771e1c85d4bf85b4370dc16e8588ecb48cc2a2f
@@ -6,8 +6,6 @@ module RestMyCase
6
6
 
7
7
  attr_writer :validators
8
8
 
9
- attr_reader :clear_errors
10
-
11
9
  def trial_court
12
10
  @trial_court ||= Trial::Court.new \
13
11
  Judge::Base, DefenseAttorney::Base, Validator, Context::Base
@@ -21,10 +19,6 @@ module RestMyCase
21
19
  @target_name = target_name
22
20
  end
23
21
 
24
- def clear_errors!
25
- @clear_errors = true
26
- end
27
-
28
22
  def validators
29
23
  @validators ||= Hash.new { |hash, key| hash[key] = [] }
30
24
  end
@@ -46,12 +40,8 @@ module RestMyCase
46
40
  protected ######################## PROTECTED #############################
47
41
 
48
42
  def store_validators_by_attribute(validator)
49
- if validator.respond_to?(:attributes) && !validator.attributes.empty?
50
- validator.attributes.each do |attribute|
51
- validators[attribute.to_sym] << validator
52
- end
53
- else
54
- validators[nil] << validator
43
+ validator.attributes.each do |attribute|
44
+ validators[attribute.to_sym] << validator
55
45
  end
56
46
  end
57
47
 
@@ -77,15 +67,15 @@ module RestMyCase
77
67
  end
78
68
 
79
69
  def target
70
+ return nil if target_name.nil?
71
+
80
72
  respond_to?(target_name) ? send(target_name) : context.send(target_name)
81
73
  end
82
74
 
83
75
  def perform
84
76
  targets = [*target]
85
77
 
86
- skip! if targets.empty?
87
-
88
- if target.nil?
78
+ if targets.empty?
89
79
  error('no target to validate!')
90
80
  else
91
81
  error('unprocessable_entity') unless all_validations_green? targets
@@ -111,8 +101,6 @@ module RestMyCase
111
101
  def extend_errors_and_run_validations(object_to_validate)
112
102
  extend_errors_if_necessary object_to_validate
113
103
 
114
- object_to_validate.errors.clear if self.class.clear_errors
115
-
116
104
  self.class.validators.values.flatten.uniq.each do |validator|
117
105
  next if validator_condition_fails(validator, object_to_validate)
118
106
 
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = '1.9.2'
3
+ VERSION = '1.9.3'
4
4
 
5
5
  end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe RestMyCase::Validator do
4
+
5
+ context "Given a validator with no target" do
6
+ before do
7
+ @context = RestMyCase::Validator.perform
8
+ end
9
+
10
+ it "@context.ok? should be false" do
11
+ expect(@context.ok?).to be false
12
+ end
13
+
14
+ it "@context.errors should reflect the fact that no target is defined" do
15
+ expect(@context.errors).to a_hash_including({"RestMyCase::Validator"=>["no target to validate!"]})
16
+ end
17
+ end
18
+
19
+ context "Given a validator with a custom validation" do
20
+ context "given a post with an invalid phone_number" do
21
+ before do
22
+ @post = RubyPost.new(phone_number: 'asd')
23
+ @context = CustomValidator.perform(post: @post)
24
+ end
25
+
26
+ it "@context.ok? should be false" do
27
+ expect(@context.ok?).to be false
28
+ end
29
+
30
+ it "@context.errors should include the unprocessable_entity error" do
31
+ expect(@context.errors).to a_hash_including({"CustomValidator"=>["unprocessable_entity"]})
32
+ end
33
+
34
+ it "@post.errors should mention the bad phone_number error" do
35
+ expect(@post.errors.added?(:phone_number, 'invalid country code')).to be true
36
+ end
37
+ end
38
+
39
+ context "given a post with a valid phone_number" do
40
+ before do
41
+ @post = RubyPost.new(phone_number: '123 123')
42
+ @context = CustomValidator.perform(post: @post)
43
+ end
44
+
45
+ it "@context.ok? should be true" do
46
+ expect(@context.ok?).to be true
47
+ end
48
+ end
49
+ end
50
+
51
+ context "Given a class that inherits from a class that has its own dependencies" do
52
+ before do
53
+ @post = RubyPost.new
54
+ @context = HierarchyValidation::Son.perform(post: @post)
55
+ end
56
+
57
+ it "HierarchyValidation::Son should be able to inherit Father's validations and alter them" do
58
+ expect(@context.ok?).to be false
59
+ expect(@post.errors.added?(:title, :blank)).to be true
60
+ expect(@post.errors.added?(:email, :blank)).to be true
61
+ expect(@post.errors.size).to be 2
62
+ end
63
+ end
64
+
65
+ end
66
+
@@ -0,0 +1,17 @@
1
+ class CustomValidator < RestMyCase::Validator
2
+
3
+ target :post
4
+
5
+ validate :phone_number_country_code
6
+
7
+ def phone_number_country_code(post)
8
+ if post.phone_number.split(' ')[0] != '123'
9
+ post.errors.add(:phone_number, 'invalid country code')
10
+
11
+ return false
12
+ end
13
+
14
+ true
15
+ end
16
+
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_my_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.2
4
+ version: 1.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - goncalvesjoao
@@ -150,13 +150,14 @@ files:
150
150
  - spec/rest_my_case/http_status_spec.rb
151
151
  - spec/rest_my_case/status_spec.rb
152
152
  - spec/rest_my_case/trial_case/court_spec.rb
153
- - spec/rest_my_case/validator/hierarchy_spec.rb
153
+ - spec/rest_my_case/validator_spec.rb
154
154
  - spec/spec_helper.rb
155
155
  - spec/support/defense_attorney.rb
156
156
  - spec/support/perform.rb
157
157
  - spec/support/rest_my_case_base.rb
158
158
  - spec/support/validator/models/ruby_post.rb
159
159
  - spec/support/validator/models/ruby_post_with_comments.rb
160
+ - spec/support/validator/usecases/custom_validator.rb
160
161
  - spec/support/validator/usecases/hierarchy_validation.rb
161
162
  homepage: https://github.com/goncalvesjoao/rest_my_case
162
163
  licenses:
@@ -191,11 +192,12 @@ test_files:
191
192
  - spec/rest_my_case/http_status_spec.rb
192
193
  - spec/rest_my_case/status_spec.rb
193
194
  - spec/rest_my_case/trial_case/court_spec.rb
194
- - spec/rest_my_case/validator/hierarchy_spec.rb
195
+ - spec/rest_my_case/validator_spec.rb
195
196
  - spec/spec_helper.rb
196
197
  - spec/support/defense_attorney.rb
197
198
  - spec/support/perform.rb
198
199
  - spec/support/rest_my_case_base.rb
199
200
  - spec/support/validator/models/ruby_post.rb
200
201
  - spec/support/validator/models/ruby_post_with_comments.rb
202
+ - spec/support/validator/usecases/custom_validator.rb
201
203
  - spec/support/validator/usecases/hierarchy_validation.rb
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'Testing hierarchy capabilities' do
4
-
5
- it "HierarchyValidation::Son should be able to inherit Father's validations and alter them" do
6
- # post_for_father = RubyPost.new
7
- # father_context = HierarchyValidation::Father.perform(post: post_for_father)
8
-
9
- # expect(father_context.ok?).to be false
10
- # expect(post_for_father.errors.added?(:title, :blank)).to be true
11
- # expect(post_for_father.errors.size).to be 1
12
-
13
- post_for_son = RubyPost.new
14
- son_context = HierarchyValidation::Son.perform(post: post_for_son)
15
-
16
- expect(son_context.ok?).to be false
17
- expect(post_for_son.errors.added?(:title, :blank)).to be true
18
- expect(post_for_son.errors.added?(:email, :blank)).to be true
19
- expect(post_for_son.errors.size).to be 2
20
- end
21
-
22
- end
23
-