action_logic 0.0.6 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,8 @@ module ActionLogic
15
15
  module ClassMethods
16
16
  def execute(params = {})
17
17
  around(params) do |execution_context|
18
+ raise ActionLogic::InvalidUseCaseError.new("ActionUseCase requires at least one ActionTask") if execution_context.tasks.empty?
19
+
18
20
  execution_context.call
19
21
 
20
22
  execution_context.tasks.reduce(execution_context.context) do |context, task|
@@ -1,6 +1,18 @@
1
1
  module ActionLogic
2
+ # Thrown whenever an ActionTask, ActionUseCase or ActionCoordinator's context does not have a key defined for the attribute key in a validations block
2
3
  class MissingAttributeError < StandardError; end
4
+
5
+ # Thrown whenever an ActionTask, ActionUseCase or ActionCoordinator's context has an attribute and value but the value's type is not the same as that
6
+ # attributey's type specified in a validations block
3
7
  class AttributeTypeError < StandardError; end
8
+
9
+ # Thrown whenever an ActionTask, ActionUseCase or ActionCoordinator's context has an attribute and value but the value definition of presence is not satisfied
10
+ # for the value stored on the context
4
11
  class PresenceError < StandardError; end
12
+
13
+ # Adding a custom presence definition is possible, but the presence validation will throw an error if the custom presence definition is not a Proc
5
14
  class UnrecognizablePresenceValidatorError < StandardError; end
15
+
16
+ # ActionUseCases are invalid if they do not define any tasks
17
+ class InvalidUseCaseError < StandardError; end
6
18
  end
@@ -1,3 +1,3 @@
1
1
  module ActionLogic
2
- VERSION = '0.0.6'
2
+ VERSION = '0.1.0'
3
3
  end
Binary file
Binary file
Binary file
@@ -32,6 +32,69 @@ module ActionLogic
32
32
  expect(result.second).to eq("defined in use case")
33
33
  end
34
34
 
35
+ describe "missing tasks" do
36
+ it "raises error if no tasks are defined" do
37
+ expect { NoTaskTestUseCase.execute() }.to\
38
+ raise_error(ActionLogic::InvalidUseCaseError)
39
+ end
40
+ end
41
+
42
+ describe "around validations" do
43
+ describe "required attributes and type validation" do
44
+ it "does not raise error if context has required keys and values are of the correct type" do
45
+ expect { ValidateAroundTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error
46
+ end
47
+
48
+ it "raises error if context is missing required keys" do
49
+ expect { ValidateAroundTestUseCase.execute() }.to\
50
+ raise_error(ActionLogic::MissingAttributeError)
51
+ end
52
+
53
+ it "raises error if context has required keys but values are not of correct type" do
54
+ expect { ValidateAroundTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to\
55
+ raise_error(ActionLogic::AttributeTypeError)
56
+ end
57
+ end
58
+
59
+ describe "custom types" do
60
+ it "allows validation against custom defined types" do
61
+ expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType1.new) }.to_not raise_error
62
+ end
63
+
64
+ it "raises error if context has custom type attribute but value is not correct custom type" do
65
+ expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType2.new) }.to\
66
+ raise_error(ActionLogic::AttributeTypeError)
67
+ end
68
+ end
69
+
70
+ describe "presence" do
71
+ it "validates presence if presence is specified" do
72
+ expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error
73
+ end
74
+
75
+ it "raises error if context has required key but value is not defined when validation requires presence" do
76
+ expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => nil) }.to\
77
+ raise_error(ActionLogic::PresenceError)
78
+ end
79
+ end
80
+
81
+ describe "custom presence" do
82
+ it "allows custom presence validation if custom presence is defined" do
83
+ expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error
84
+ end
85
+
86
+ it "raises error if custom presence validation is not satisfied" do
87
+ expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => []) }.to\
88
+ raise_error(ActionLogic::PresenceError)
89
+ end
90
+
91
+ it "raises error if custom presence validation is not supported" do
92
+ expect { ValidateAroundUnrecognizablePresenceTestUseCase.execute(:integer_test => 1) }.to\
93
+ raise_error(ActionLogic::UnrecognizablePresenceValidatorError)
94
+ end
95
+ end
96
+ end
97
+
35
98
  describe "before validations" do
36
99
  describe "required attributes and type validation" do
37
100
  it "does not raise error if context has required keys and values are of the correct type" do
@@ -37,6 +37,87 @@ class SimpleTestUseCase3
37
37
  end
38
38
  end
39
39
 
40
+ class NoTaskTestUseCase
41
+ include ActionLogic::ActionUseCase
42
+
43
+ def call
44
+ end
45
+
46
+ def tasks
47
+ []
48
+ end
49
+ end
50
+
51
+ class ValidateAroundTestUseCase
52
+ include ActionLogic::ActionUseCase
53
+
54
+ validates_around Constants::ALL_VALIDATIONS
55
+
56
+ def call
57
+ end
58
+
59
+ def tasks
60
+ [UseCaseTestTask1,
61
+ UseCaseTestTask2]
62
+ end
63
+ end
64
+
65
+ class ValidateAroundCustomTypeTestUseCase
66
+ include ActionLogic::ActionUseCase
67
+
68
+ validates_around :custom_type => { :type => :customtype1, :presence => true }
69
+
70
+ def call
71
+ end
72
+
73
+ def tasks
74
+ [UseCaseTestTask1,
75
+ UseCaseTestTask2]
76
+ end
77
+ end
78
+
79
+ class ValidateAroundUnrecognizablePresenceTestUseCase
80
+ include ActionLogic::ActionUseCase
81
+
82
+ validates_around :integer_test => { :presence => :true }
83
+
84
+ def call
85
+ end
86
+
87
+ def tasks
88
+ [UseCaseTestTask1,
89
+ UseCaseTestTask2]
90
+ end
91
+ end
92
+
93
+ class ValidateAroundPresenceTestUseCase
94
+ include ActionLogic::ActionUseCase
95
+
96
+ validates_around :integer_test => { :presence => true }
97
+
98
+ def call
99
+ end
100
+
101
+ def tasks
102
+ [UseCaseTestTask1,
103
+ UseCaseTestTask2]
104
+ end
105
+ end
106
+
107
+ class ValidateAroundCustomPresenceTestUseCase
108
+ include ActionLogic::ActionUseCase
109
+
110
+ validates_around :array_test => { :presence => ->(array_test) { array_test.any? } }
111
+
112
+ def call
113
+ end
114
+
115
+ def tasks
116
+ [UseCaseTestTask1,
117
+ UseCaseTestTask2]
118
+ end
119
+ end
120
+
40
121
  class ValidateBeforeTestUseCase
41
122
  include ActionLogic::ActionUseCase
42
123
 
@@ -46,7 +127,8 @@ class ValidateBeforeTestUseCase
46
127
  end
47
128
 
48
129
  def tasks
49
- []
130
+ [UseCaseTestTask1,
131
+ UseCaseTestTask2]
50
132
  end
51
133
  end
52
134
 
@@ -59,7 +141,8 @@ class ValidateBeforePresenceTestUseCase
59
141
  end
60
142
 
61
143
  def tasks
62
- []
144
+ [UseCaseTestTask1,
145
+ UseCaseTestTask2]
63
146
  end
64
147
  end
65
148
 
@@ -72,7 +155,8 @@ class ValidateBeforeCustomPresenceTestUseCase
72
155
  end
73
156
 
74
157
  def tasks
75
- []
158
+ [UseCaseTestTask1,
159
+ UseCaseTestTask2]
76
160
  end
77
161
  end
78
162
 
@@ -85,7 +169,8 @@ class ValidateBeforeCustomTypeTestUseCase
85
169
  end
86
170
 
87
171
  def tasks
88
- []
172
+ [UseCaseTestTask1,
173
+ UseCaseTestTask2]
89
174
  end
90
175
  end
91
176
 
@@ -98,7 +183,8 @@ class ValidateBeforeUnrecognizablePresenceTestUseCase
98
183
  end
99
184
 
100
185
  def tasks
101
- []
186
+ [UseCaseTestTask1,
187
+ UseCaseTestTask2]
102
188
  end
103
189
  end
104
190
 
@@ -119,7 +205,7 @@ class ValidateAfterTestUseCase
119
205
  end
120
206
 
121
207
  def tasks
122
- []
208
+ [UseCaseTestTask3]
123
209
  end
124
210
  end
125
211
 
@@ -132,7 +218,8 @@ class ValidateAfterMissingAttributesTestUseCase
132
218
  end
133
219
 
134
220
  def tasks
135
- []
221
+ [UseCaseTestTask1,
222
+ UseCaseTestTask2]
136
223
  end
137
224
  end
138
225
 
@@ -153,7 +240,8 @@ class ValidateAfterInvalidTypeTestUseCase
153
240
  end
154
241
 
155
242
  def tasks
156
- []
243
+ [UseCaseTestTask1,
244
+ UseCaseTestTask2]
157
245
  end
158
246
  end
159
247
 
@@ -167,7 +255,8 @@ class ValidateAfterCustomTypeTestUseCase
167
255
  end
168
256
 
169
257
  def tasks
170
- []
258
+ [UseCaseTestTask1,
259
+ UseCaseTestTask2]
171
260
  end
172
261
  end
173
262
 
@@ -181,7 +270,8 @@ class ValidateAfterInvalidCustomTypeTestUseCase
181
270
  end
182
271
 
183
272
  def tasks
184
- []
273
+ [UseCaseTestTask1,
274
+ UseCaseTestTask2]
185
275
  end
186
276
  end
187
277
 
@@ -195,7 +285,7 @@ class ValidateAfterPresenceTestUseCase
195
285
  end
196
286
 
197
287
  def tasks
198
- []
288
+ [UseCaseTestTask3]
199
289
  end
200
290
  end
201
291
 
@@ -209,7 +299,8 @@ class ValidateAfterInvalidPresenceTestUseCase
209
299
  end
210
300
 
211
301
  def tasks
212
- []
302
+ [UseCaseTestTask1,
303
+ UseCaseTestTask2]
213
304
  end
214
305
  end
215
306
 
@@ -223,7 +314,8 @@ class ValidateAfterCustomPresenceTestUseCase
223
314
  end
224
315
 
225
316
  def tasks
226
- []
317
+ [UseCaseTestTask1,
318
+ UseCaseTestTask2]
227
319
  end
228
320
  end
229
321
 
@@ -237,7 +329,8 @@ class ValidateAfterInvalidCustomPresenceTestUseCase
237
329
  end
238
330
 
239
331
  def tasks
240
- []
332
+ [UseCaseTestTask1,
333
+ UseCaseTestTask2]
241
334
  end
242
335
  end
243
336
 
@@ -251,7 +344,8 @@ class ValidateAfterUnrecognizablePresenceTestUseCase
251
344
  end
252
345
 
253
346
  def tasks
254
- []
347
+ [UseCaseTestTask1,
348
+ UseCaseTestTask2]
255
349
  end
256
350
  end
257
351
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_logic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Winfrey
@@ -73,6 +73,7 @@ extensions: []
73
73
  extra_rdoc_files: []
74
74
  files:
75
75
  - ".gitignore"
76
+ - CODE_OF_CONDUCT.md
76
77
  - Gemfile
77
78
  - Gemfile.lock
78
79
  - README.md
@@ -86,6 +87,11 @@ files:
86
87
  - lib/action_logic/action_validation.rb
87
88
  - lib/action_logic/errors.rb
88
89
  - lib/action_logic/version.rb
90
+ - resources/action_coordinator_diagram.png
91
+ - resources/action_task_diagram.png
92
+ - resources/action_use_case_diagram.png
93
+ - resources/diagrams.sketch
94
+ - resources/overview_diagram.png
89
95
  - spec/action_logic/action_context_spec.rb
90
96
  - spec/action_logic/action_coordinator_spec.rb
91
97
  - spec/action_logic/action_task_spec.rb