rest_my_case 1.3.5 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: befb7a3cb43ce0e61ec02c2fc33afe9e1d9f734b
4
- data.tar.gz: 18ac6c5fcf8ddb9a3d8534e7697fd1ae7995d299
3
+ metadata.gz: 4247a51223096c120fdb4055309c9fbbabd8517f
4
+ data.tar.gz: a3c6837847258f454c8cb604ceed36a1e836c1ef
5
5
  SHA512:
6
- metadata.gz: 74deb7063872d6ce4f3a0ed098238b70c27f671f6611ccf291c8facfbe31d5f33f4b18fb368ec04de1b818453c514332422b4e4720fd06d20fa33f90ad6e6529
7
- data.tar.gz: 57cce6814654046a2a9700952aff2dace3fceb10abb4d804d788facd9263bd9c17a294b802e255c6341e0426085c01efa47f55d21e015c54311de0c62d5357ad
6
+ metadata.gz: 6d7663942c2b7324ea877778295c87a19c7a18fdacb022e16136ca71b35d969b9beb473d8142a06bc9e9ef5b50540d93db4f3673232e2fd9c84700db8d6422a3
7
+ data.tar.gz: 1a39bb5b4b29d394c71179cba26052066e404d418026c3c150475cb6980a99803cb70503d5ff3542b3ea16346193db95bbb8978b51a8d31c0d9172694e007da4
data/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
4
+
5
+ Encoding.default_external = Encoding::UTF_8
6
+ Encoding.default_internal = Encoding::UTF_8
7
+
8
+ require 'rubygems'
9
+ require 'bundler'
10
+
11
+ Bundler.require(:default, 'development')
12
+
13
+
14
+ require 'spec/support/perform'
15
+
16
+ binding.pry
data/lib/rest_my_case.rb CHANGED
@@ -1,16 +1,19 @@
1
1
  require 'ostruct'
2
2
 
3
- require "rest_my_case/config/shared"
3
+ require "rest_my_case/version"
4
+ require "rest_my_case/errors"
5
+
4
6
  require "rest_my_case/config/base"
5
7
  require "rest_my_case/config/general"
6
8
 
7
- require "rest_my_case/judge/base"
8
- require "rest_my_case/trial_case/base"
9
- require "rest_my_case/trial_case/context"
10
9
  require "rest_my_case/defense_attorney/base"
10
+ require "rest_my_case/context/base"
11
+ require "rest_my_case/judge/base"
12
+
13
+ require "rest_my_case/trial/defendant"
14
+ require "rest_my_case/trial/case"
15
+ require "rest_my_case/trial/court"
11
16
 
12
- require "rest_my_case/version"
13
- require "rest_my_case/errors"
14
17
  require "rest_my_case/base"
15
18
 
16
19
  module RestMyCase
@@ -4,24 +4,24 @@ module RestMyCase
4
4
 
5
5
  extend Config::Base
6
6
 
7
- def self.depends(*use_cases)
8
- dependencies.push *use_cases
7
+ TRIAL_COURT = Trial::Court.new Judge::Base, DefenseAttorney::Base
8
+
9
+ def self.depends(*use_case_classes)
10
+ dependencies.push *use_case_classes
9
11
  end
10
12
 
11
13
  def self.dependencies
12
14
  @dependencies ||= []
13
15
  end
14
16
 
15
- def self.perform(attributes = {})
16
- trial_case = TrialCase::Base.new self, attributes
17
- attorney = DefenseAttorney::Base.new trial_case
18
- judge = Judge::Base.new trial_case
19
-
20
- attorney.build_case_for_the_defendant
17
+ def self.perform(attributes = nil)
18
+ attributes ||= {}
21
19
 
22
- judge.execute_the_sentence
20
+ unless attributes.respond_to?(:to_hash)
21
+ raise ArgumentError.new('Must respond_to method #to_hash')
22
+ end
23
23
 
24
- trial_case.context
24
+ TRIAL_COURT.execute([self], attributes.to_hash).context
25
25
  end
26
26
 
27
27
  def self.context_accessor(*methods)
@@ -39,13 +39,14 @@ module RestMyCase
39
39
  methods.each { |method| define_method(method) { context.send(method) } }
40
40
  end
41
41
 
42
- ##################### INSTANCE METHODS BELLOW ###################
42
+ ######################## INSTANCE METHODS BELLOW ###########################
43
43
 
44
- attr_reader :context, :options
44
+ attr_reader :context, :dependent_use_case, :options
45
45
 
46
- def initialize(context, options = {})
47
- @context = context
48
- @options = options.dup
46
+ def initialize(context, dependent_use_case = nil)
47
+ @options = {}
48
+ @context = context
49
+ @dependent_use_case = dependent_use_case
49
50
  end
50
51
 
51
52
  def setup; end
@@ -56,8 +57,18 @@ module RestMyCase
56
57
 
57
58
  def final; end
58
59
 
60
+ def invoke(*use_case_classes)
61
+ TRIAL_COURT.execute(use_case_classes, context.to_hash).context
62
+ end
63
+
64
+ def invoke!(*use_case_classes)
65
+ TRIAL_COURT.execute(use_case_classes, context).tap do |trial_case|
66
+ abort if trial_case.aborted
67
+ end.context
68
+ end
69
+
59
70
  def abort
60
- options[:should_abort] = true
71
+ silent_abort? ? dependent_use_case.abort : (options[:should_abort] = true)
61
72
  end
62
73
 
63
74
  def abort!
@@ -80,6 +91,14 @@ module RestMyCase
80
91
  skip && raise(Errors::Skip)
81
92
  end
82
93
 
94
+ protected ######################## PROTECTED ###############################
95
+
96
+ def silent_abort?
97
+ return false if dependent_use_case.nil?
98
+
99
+ RestMyCase.get_config :silence_dependencies_abort, dependent_use_case.class
100
+ end
101
+
83
102
  end
84
103
 
85
104
  end
@@ -3,8 +3,6 @@ module RestMyCase
3
3
 
4
4
  module Base
5
5
 
6
- include Shared
7
-
8
6
  attr_accessor :silence_dependencies_abort
9
7
 
10
8
  end
@@ -3,15 +3,14 @@ module RestMyCase
3
3
 
4
4
  class General
5
5
 
6
- include Shared
6
+ include Base
7
7
 
8
8
  def initialize
9
- @dependencies_first = true
10
- @parent_dependencies_first = true
9
+ @silence_dependencies_abort = false
11
10
  end
12
11
 
13
- def get(attribute, use_case)
14
- custom_config = use_case.send(attribute)
12
+ def get(attribute, use_case_class)
13
+ custom_config = use_case_class.send(attribute)
15
14
 
16
15
  custom_config.nil? ? send(attribute) : custom_config
17
16
  end
@@ -1,7 +1,15 @@
1
1
  module RestMyCase
2
- module TrialCase
2
+ module Context
3
3
 
4
- class Context < OpenStruct
4
+ class Base < OpenStruct
5
+
6
+ alias :attributes :marshal_dump
7
+
8
+ include ActiveModel::Serialization if defined?(ActiveModel)
9
+
10
+ def to_hash
11
+ Marshal.load Marshal.dump(attributes)
12
+ end
5
13
 
6
14
  def errors
7
15
  @errors ||= Hash.new { |hash, key| hash[key] = [] }
@@ -13,12 +21,6 @@ module RestMyCase
13
21
 
14
22
  alias :ok? :valid?
15
23
 
16
- def serializable_hash(options = nil)
17
- marshal_dump
18
- end
19
-
20
- alias :to_hash :serializable_hash
21
-
22
24
  end
23
25
 
24
26
  end
@@ -8,51 +8,35 @@ module RestMyCase
8
8
  end
9
9
 
10
10
  def build_case_for_the_defendant
11
- @trial_case.use_cases = all_dependencies @trial_case.defendant
11
+ @trial_case.use_cases =
12
+ @trial_case.defendant.use_case_classes.map do |use_case_class|
13
+ all_dependencies(use_case_class)
14
+ end.flatten
12
15
  end
13
16
 
14
- protected ###################### PROTECTED #########################
17
+ protected ######################## PROTECTED #############################
15
18
 
16
- def all_dependencies(use_case, options = {})
17
- return [] unless use_case.respond_to?(:dependencies)
19
+ def all_dependencies(use_case_class)
20
+ return [] unless use_case_class.respond_to? :dependencies
18
21
 
19
- parent_dependencies = all_dependencies(use_case.superclass, options)
20
-
21
- if RestMyCase.get_config(:parent_dependencies_first, use_case)
22
- parent_dependencies | deep_dependencies(use_case, options)
23
- else
24
- deep_dependencies(use_case, options) | parent_dependencies
25
- end
22
+ all_dependencies(use_case_class.superclass) |
23
+ dependencies_including_itself_last(use_case_class, nil)
26
24
  end
27
25
 
28
- def deep_dependencies(use_case, options)
29
- options = build_options(use_case, options)
30
- deep_dependencies = []
31
-
32
- use_case.dependencies.each do |dependency|
33
- deep_dependencies.push *all_dependencies(dependency, options)
34
- end
35
-
36
- include_current_use_case(deep_dependencies, use_case, options)
37
- end
26
+ private ########################### PRIVATE ##############################
38
27
 
39
- private ####################### PRIVATE ###########################
28
+ def dependencies_including_itself_last(use_case_class, dependent_use_case)
29
+ return [] unless use_case_class.superclass.respond_to? :dependencies
40
30
 
41
- def build_options(use_case, options)
42
- unless use_case.silence_dependencies_abort.nil?
43
- options[:silent_abort] = use_case.silence_dependencies_abort
44
- end
31
+ use_case = use_case_class.new(@trial_case.context, dependent_use_case)
45
32
 
46
- options
33
+ dependencies(use_case).push use_case
47
34
  end
48
35
 
49
- def include_current_use_case(dependencies, use_case, options)
50
- return dependencies unless use_case.superclass.respond_to? :dependencies
51
-
52
- append_method =
53
- RestMyCase.get_config(:dependencies_first, use_case) ? :push : :unshift
54
-
55
- dependencies.send append_method, use_case.new(@trial_case.context, options)
36
+ def dependencies(use_case)
37
+ use_case.class.dependencies.map do |dependency|
38
+ dependencies_including_itself_last dependency, use_case
39
+ end
56
40
  end
57
41
 
58
42
  end
@@ -9,14 +9,16 @@ module RestMyCase
9
9
  @use_case_that_aborted = false
10
10
  end
11
11
 
12
- def execute_the_sentence
12
+ def determine_the_sentence
13
13
  run_setup_methods
14
14
  run_perform_methods
15
15
  run_rollback_methods
16
16
  run_final_methods
17
+
18
+ @trial_case.aborted = !!@use_case_that_aborted
17
19
  end
18
20
 
19
- protected #################### PROTECTED ####################
21
+ protected ######################## PROTECTED #############################
20
22
 
21
23
  def run_setup_methods
22
24
  @trial_case.use_cases.each do |use_case|
@@ -36,15 +38,17 @@ module RestMyCase
36
38
  return nil unless @use_case_that_aborted
37
39
 
38
40
  @performed_use_cases.reverse.each do |use_case|
39
- run_method(:rollback, use_case)
41
+ method_aborts?(:rollback, use_case)
40
42
  end
41
43
  end
42
44
 
43
45
  def run_final_methods
44
- @trial_case.use_cases.each { |use_case| run_method(:final, use_case) }
46
+ @trial_case.use_cases.each do |use_case|
47
+ method_aborts?(:final, use_case)
48
+ end
45
49
  end
46
50
 
47
- private #################### PRIVATE ######################
51
+ private ########################### PRIVATE ##############################
48
52
 
49
53
  def method_setup_has_aborted(use_case)
50
54
  method_aborts?(:setup, use_case)
@@ -60,20 +64,16 @@ module RestMyCase
60
64
 
61
65
  def method_aborts?(method_name, use_case)
62
66
  begin
63
- run_method(method_name, use_case)
67
+ use_case.send(method_name)
64
68
 
65
69
  use_case.options[:should_abort] && @use_case_that_aborted = use_case
66
- rescue Errors::Skip => exception
70
+ rescue Errors::Skip
67
71
  false
68
- rescue Errors::Abort => exception
72
+ rescue Errors::Abort
69
73
  @use_case_that_aborted = use_case
70
74
  end
71
75
  end
72
76
 
73
- def run_method(method_name, use_case)
74
- use_case.send(method_name)
75
- end
76
-
77
77
  end
78
78
 
79
79
  end
@@ -0,0 +1,28 @@
1
+ module RestMyCase
2
+ module Trial
3
+
4
+ class Case
5
+
6
+ attr_accessor :use_cases, :aborted
7
+
8
+ attr_reader :context, :defendant
9
+
10
+ def initialize(use_case_classes, attributes)
11
+ @aborted = false
12
+ @context = build_context(attributes)
13
+ @defendant = Defendant.new use_case_classes
14
+ @use_cases = []
15
+ end
16
+
17
+ protected ######################## PROTECTED #############################
18
+
19
+ def build_context(attributes)
20
+ return attributes if attributes.is_a?(Context::Base)
21
+
22
+ Context::Base.new attributes
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module RestMyCase
2
+ module Trial
3
+
4
+ class Court < Struct.new(:judge_class, :defense_attorney_class)
5
+
6
+ def execute(use_case_classes, attributes = {})
7
+ trial_case = Case.new(use_case_classes, attributes)
8
+
9
+ defense_attorney_class.new(trial_case).build_case_for_the_defendant
10
+
11
+ judge_class.new(trial_case).determine_the_sentence
12
+
13
+ trial_case
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module RestMyCase
2
+ module Trial
3
+
4
+ class Defendant < Struct.new(:use_case_classes)
5
+ end
6
+
7
+ end
8
+ end
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = "1.3.5"
3
+ VERSION = "1.5.0"
4
4
 
5
5
  end
@@ -3,15 +3,18 @@ require 'spec_helper'
3
3
  describe RestMyCase::Base do
4
4
 
5
5
  describe ".dependencies" do
6
+
6
7
  it "should only list the class's dependencies" do
7
8
  expect(RestMyCaseBase::CreatePostWithComments.dependencies).to \
8
9
  eq [RestMyCaseBase::BuildComments, RestMyCaseBase::CreateComments]
9
10
  end
11
+
10
12
  end
11
13
 
12
14
  describe ".context_accessor" do
13
- let(:context) { RestMyCase::TrialCase::Context.new(id: 1, comment: 'my comment', session: -1) }
14
- let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
15
+
16
+ let(:context) { RestMyCase::Context::Base.new(id: 1, comment: 'my comment', session: -1) }
17
+ let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
15
18
 
16
19
  it "Should create getters targeting to context" do
17
20
  expect(use_case.respond_to?(:comment)).to be true
@@ -29,11 +32,13 @@ describe RestMyCase::Base do
29
32
  use_case.comment = 'your comment'
30
33
  expect(use_case.context.comment).to eq 'your comment'
31
34
  end
35
+
32
36
  end
33
37
 
34
38
  describe ".context_writer" do
35
- let(:context) { RestMyCase::TrialCase::Context.new }
36
- let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
39
+
40
+ let(:context) { RestMyCase::Context::Base.new }
41
+ let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
37
42
 
38
43
  it "Should create setters targeting to context" do
39
44
  expect(use_case.respond_to?(:id)).to be false
@@ -45,11 +50,13 @@ describe RestMyCase::Base do
45
50
 
46
51
  expect(use_case.context.id).to eq 2
47
52
  end
53
+
48
54
  end
49
55
 
50
56
  describe ".context_reader" do
51
- let(:context) { RestMyCase::TrialCase::Context.new(id: 1, comment: 'my comment', session: -1) }
52
- let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
57
+
58
+ let(:context) { RestMyCase::Context::Base.new(id: 1, comment: 'my comment', session: -1) }
59
+ let(:use_case) { RestMyCaseBase::CreatePostWithComments.new(context) }
53
60
 
54
61
  it "Should create getters targeting to context" do
55
62
  expect(use_case.respond_to?(:session)).to be true
@@ -59,26 +66,40 @@ describe RestMyCase::Base do
59
66
  it "Getter should delegate to context" do
60
67
  expect(use_case.session).to eq -1
61
68
  end
69
+
62
70
  end
63
71
 
64
72
  describe ".perform" do
65
73
 
66
- context "When a use case #fail during the setup process" do
67
- before do
68
- class Perform::ValidateName < RestMyCase::Base
69
- def setup
70
- fail('no name!')
71
- context.setup << self.class.name
72
- end
73
- end
74
+ context "When something that doesn't responds to #to_hash is passed down" do
74
75
 
75
- @context = Perform::CreatePost.perform
76
+ it "should raise an exception" do
77
+ expect { Perform::CreatePost.perform(Object.new) }.to \
78
+ raise_error(ArgumentError)
76
79
  end
77
80
 
78
- after do
79
- class Perform::ValidateName < RestMyCase::Base
80
- def setup; context.setup << self.class.name; end
81
- end
81
+ end
82
+
83
+ context "When nil is passed down" do
84
+
85
+ it "should NOT raise an exception" do
86
+ expect { Perform::CreatePost.perform(nil) }.not_to raise_error
87
+ end
88
+
89
+ end
90
+
91
+ context "When nothing is passed down" do
92
+
93
+ it "should NOT raise an exception" do
94
+ expect { Perform::CreatePost.perform }.not_to raise_error
95
+ end
96
+
97
+ end
98
+
99
+ context "When a use case calls #fail during the setup process" do
100
+ before do
101
+ @context = Perform::CreatePost.perform \
102
+ fail: ['Perform::ValidateName_setup', 'Perform::ValidateBody_setup']
82
103
  end
83
104
 
84
105
  it "context should reflect an invalid state" do
@@ -90,29 +111,17 @@ describe RestMyCase::Base do
90
111
  end
91
112
 
92
113
  it "context prove that only the correct method have ran" do
93
- expect(@context.setup.length).to be 2
114
+ expect(@context.setup.length).to be 3
94
115
  expect(@context.perform.length).to be 0
95
116
  expect(@context.rollback.length).to be 0
96
- expect(@context.final.length).to be 6
117
+ expect(@context.final.length).to be 7
97
118
  end
98
119
  end
99
120
 
100
- context "When a use case #fail! during the setup process" do
121
+ context "When a use case calls #fail! during the setup process" do
101
122
  before do
102
- class Perform::ValidateName < RestMyCase::Base
103
- def setup
104
- fail!('no name!')
105
- context.setup << self.class.name
106
- end
107
- end
108
-
109
- @context = Perform::CreatePost.perform
110
- end
111
-
112
- after do
113
- class Perform::ValidateName < RestMyCase::Base
114
- def setup; context.setup << self.class.name; end
115
- end
123
+ @context = Perform::CreatePost.perform \
124
+ fail_bang: ['Perform::ValidateName_setup']
116
125
  end
117
126
 
118
127
  it "context should reflect an invalid state" do
@@ -124,29 +133,72 @@ describe RestMyCase::Base do
124
133
  end
125
134
 
126
135
  it "context prove that only the correct method have ran" do
127
- expect(@context.setup.length).to be 1
136
+ expect(@context.setup.length).to be 2
128
137
  expect(@context.perform.length).to be 0
129
138
  expect(@context.rollback.length).to be 0
130
- expect(@context.final.length).to be 6
139
+ expect(@context.final.length).to be 7
131
140
  end
132
141
  end
133
142
 
134
- context "When a use case #skip during the setup process" do
143
+ context "When a use case calls #abort! during the setup process" do
135
144
  before do
136
- class Perform::ValidateBody < RestMyCase::Base
137
- def setup
138
- skip
139
- context.setup << self.class.name
140
- end
141
- end
145
+ @context = Perform::CreatePost.perform \
146
+ abort_bang: ['Perform::ValidateName_setup']
147
+ end
142
148
 
143
- @context = Perform::CreatePost.perform
149
+ it "context should reflect an valid state" do
150
+ expect(@context.valid?).to be true
144
151
  end
145
152
 
146
- after do
147
- class Perform::ValidateName < RestMyCase::Base
148
- def setup; context.setup << self.class.name; end
149
- end
153
+ it "context prove that only the correct method have ran" do
154
+ expect(@context.setup.length).to be 2
155
+ expect(@context.perform.length).to be 0
156
+ expect(@context.rollback.length).to be 0
157
+ expect(@context.final.length).to be 7
158
+ end
159
+ end
160
+
161
+ context "When a use case calls #abort! during the perform process" do
162
+ before do
163
+ @context = Perform::CreatePost.perform \
164
+ abort_bang: ['Perform::ValidateName_perform']
165
+ end
166
+
167
+ it "context should reflect an valid state" do
168
+ expect(@context.valid?).to be true
169
+ end
170
+
171
+ it "context prove that only the correct method have ran" do
172
+ expect(@context.setup.length).to be 7
173
+ expect(@context.perform.length).to be 2
174
+ expect(@context.rollback.length).to be 3
175
+ expect(@context.final.length).to be 7
176
+ end
177
+ end
178
+
179
+ context "When a use case calls #skip during the setup process" do
180
+ before do
181
+ @context = Perform::CreatePost.perform \
182
+ skip: ['Perform::ValidateBody_setup']
183
+ end
184
+
185
+ it "context should reflect an valid state" do
186
+ expect(@context.valid?).to be true
187
+ end
188
+
189
+ it "context prove that only the correct method have ran" do
190
+ expect(@context.setup.length).to be 7
191
+ expect(@context.perform.length).to be 6
192
+ expect(@context.rollback.length).to be 0
193
+ expect(@context.final.length).to be 7
194
+ end
195
+ end
196
+
197
+ context "When a use case calls #skip! during the setup process" do
198
+
199
+ before do
200
+ @context = Perform::CreatePost.perform \
201
+ skip_bang: ['Perform::ValidateBody_setup']
150
202
  end
151
203
 
152
204
  it "context should reflect an valid state" do
@@ -155,10 +207,237 @@ describe RestMyCase::Base do
155
207
 
156
208
  it "context prove that only the correct method have ran" do
157
209
  expect(@context.setup.length).to be 6
158
- expect(@context.perform.length).to be 5
210
+ expect(@context.perform.length).to be 6
159
211
  expect(@context.rollback.length).to be 0
160
- expect(@context.final.length).to be 6
212
+ expect(@context.final.length).to be 7
213
+ end
214
+
215
+ end
216
+
217
+ context "When dependent use_case has silence_dependencies_abort = true" do
218
+
219
+ before { Perform::Validations.silence_dependencies_abort = true }
220
+ after { Perform::Validations.silence_dependencies_abort = nil }
221
+
222
+ context "When a use case calls #fail during the setup process" do
223
+ before do
224
+ @context = Perform::CreatePost.perform \
225
+ fail: ['Perform::ValidateName_setup', 'Perform::ValidateBody_setup']
226
+ end
227
+
228
+ it "context should reflect an invalid state" do
229
+ expect(@context.valid?).to be false
230
+ end
231
+
232
+ it "context should contain only 2 errors" do
233
+ expect(@context.errors.keys.length).to be 2
234
+ end
235
+
236
+ it "context prove that only the correct method have ran" do
237
+ expect(@context.setup.length).to be 5
238
+ expect(@context.perform.length).to be 0
239
+ expect(@context.rollback.length).to be 0
240
+ expect(@context.final.length).to be 7
241
+ end
242
+
161
243
  end
244
+
245
+ context "When a use case calls #fail during the perform process" do
246
+
247
+ before do
248
+ @context = Perform::CreatePost.perform \
249
+ fail: ['Perform::ValidateName_perform', 'Perform::ValidateBody_perform']
250
+ end
251
+
252
+ it "context should reflect an invalid state" do
253
+ expect(@context.valid?).to be false
254
+ end
255
+
256
+ it "context should contain only 2 errors" do
257
+ expect(@context.errors.keys.length).to be 2
258
+ end
259
+
260
+ it "context prove that only the correct method have ran" do
261
+ expect(@context.setup.length).to be 7
262
+ expect(@context.perform.length).to be 5
263
+ expect(@context.rollback.length).to be 5
264
+ expect(@context.final.length).to be 7
265
+ end
266
+
267
+ end
268
+
269
+ end
270
+
271
+ context "When general config has silence_dependencies_abort = true" do
272
+
273
+ before do
274
+ RestMyCase.configure do |config|
275
+ config.silence_dependencies_abort = true
276
+ end
277
+ end
278
+ after { RestMyCase.reset_config }
279
+
280
+ context "When a use case calls #fail during the perform process" do
281
+
282
+ before do
283
+ @context = Perform::CreatePost.perform \
284
+ fail: ['Perform::ValidateName_perform', 'Perform::ValidateBody_perform', 'Perform::BuildPost_perform', 'Perform::SavePost_perform']
285
+ end
286
+
287
+ it "context should reflect an invalid state" do
288
+ expect(@context.valid?).to be false
289
+ end
290
+
291
+ it "context should contain only 4 errors" do
292
+ expect(@context.errors.keys.length).to be 4
293
+ end
294
+
295
+ it "context prove that only the correct method have ran" do
296
+ expect(@context.setup.length).to be 7
297
+ expect(@context.perform.length).to be 7
298
+ expect(@context.rollback.length).to be 7
299
+ expect(@context.final.length).to be 7
300
+ end
301
+
302
+ end
303
+
304
+ end
305
+
306
+ end
307
+
308
+ describe "#invoke" do
309
+
310
+ class Perform::Invoker < Perform::UseCaseWrapper
311
+ def perform
312
+ super
313
+ invoke Perform::BuildPost, Perform::Validations, Perform::SavePost
314
+ end
315
+ end
316
+
317
+ it "should invoke other use cases with the invoker's cloned context and they can't alter the invoker's context" do
318
+ context = Perform::Invoker.perform
319
+
320
+ expect(context.setup.length).to be 2
321
+ expect(context.perform.length).to be 2
322
+ expect(context.rollback.length).to be 0
323
+ expect(context.final.length).to be 2
324
+ end
325
+
326
+ end
327
+
328
+ describe "#invoke!" do
329
+
330
+ context "when the invokees don't abort" do
331
+
332
+ class Perform::ScreammingInvoker < Perform::UseCaseWrapper
333
+ def perform
334
+ super
335
+ invoke! Perform::BuildPost, Perform::Validations, Perform::SavePost
336
+ end
337
+ end
338
+
339
+ it "should invoke other use cases with the invoker's context and actually alter it" do
340
+ @context = Perform::ScreammingInvoker.perform
341
+
342
+ expect(@context.setup).to eq [
343
+ "Perform::UseCaseWrapper",
344
+ "Perform::ScreammingInvoker",
345
+ "Perform::UseCaseWrapper",
346
+ "Perform::BuildPost",
347
+ "Perform::UseCaseWrapper",
348
+ "Perform::ValidateName",
349
+ "Perform::ValidateBody",
350
+ "Perform::Validations",
351
+ "Perform::UseCaseWrapper",
352
+ "Perform::SavePost"
353
+ ]
354
+ expect(@context.perform).to eq [
355
+ "Perform::UseCaseWrapper",
356
+ "Perform::ScreammingInvoker",
357
+ "Perform::UseCaseWrapper",
358
+ "Perform::BuildPost",
359
+ "Perform::UseCaseWrapper",
360
+ "Perform::ValidateName",
361
+ "Perform::ValidateBody",
362
+ "Perform::Validations",
363
+ "Perform::UseCaseWrapper",
364
+ "Perform::SavePost"
365
+ ]
366
+ expect(@context.rollback).to eq []
367
+ expect(@context.final).to eq [
368
+ "Perform::UseCaseWrapper",
369
+ "Perform::BuildPost",
370
+ "Perform::UseCaseWrapper",
371
+ "Perform::ValidateName",
372
+ "Perform::ValidateBody",
373
+ "Perform::Validations",
374
+ "Perform::UseCaseWrapper",
375
+ "Perform::SavePost",
376
+ "Perform::UseCaseWrapper",
377
+ "Perform::ScreammingInvoker"
378
+ ]
379
+ end
380
+
381
+ end
382
+
383
+ context "when the invokees abort" do
384
+
385
+ class Perform::Validations2 < Perform::UseCaseWrapper
386
+ def perform
387
+ super
388
+
389
+ invoke! Perform::ValidateName, Perform::ValidateBody
390
+ end
391
+ end
392
+
393
+ class Perform::CreatePost2 < Perform::UseCaseWrapper
394
+ depends Perform::BuildPost, Perform::Validations2, Perform::SavePost
395
+ end
396
+
397
+ before do
398
+ @context = Perform::CreatePost2.perform \
399
+ fail: ['Perform::ValidateName_perform', 'Perform::ValidateBody_perform']
400
+ end
401
+
402
+ it "invoker should abort the process it his invokees have also aborted" do
403
+ expect(@context.setup).to eq [
404
+ "Perform::UseCaseWrapper",
405
+ "Perform::BuildPost",
406
+ "Perform::Validations2",
407
+ "Perform::SavePost",
408
+ "Perform::CreatePost2",
409
+ "Perform::UseCaseWrapper",
410
+ "Perform::ValidateName",
411
+ "Perform::UseCaseWrapper",
412
+ "Perform::ValidateBody"
413
+ ]
414
+ expect(@context.perform).to eq [
415
+ "Perform::UseCaseWrapper",
416
+ "Perform::BuildPost",
417
+ "Perform::Validations2",
418
+ "Perform::UseCaseWrapper",
419
+ "Perform::ValidateName"
420
+ ]
421
+ expect(@context.rollback).to eq [
422
+ "Perform::ValidateName",
423
+ "Perform::UseCaseWrapper",
424
+ "Perform::Validations2",
425
+ "Perform::BuildPost",
426
+ "Perform::UseCaseWrapper"
427
+ ]
428
+ expect(@context.final).to eq [
429
+ "Perform::UseCaseWrapper",
430
+ "Perform::ValidateName",
431
+ "Perform::UseCaseWrapper",
432
+ "Perform::ValidateBody",
433
+ "Perform::UseCaseWrapper",
434
+ "Perform::BuildPost",
435
+ "Perform::Validations2",
436
+ "Perform::SavePost",
437
+ "Perform::CreatePost2"
438
+ ]
439
+ end
440
+
162
441
  end
163
442
 
164
443
  end