smash_the_state 1.4.3 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/smash_the_state/operation/sequence.rb +12 -5
- data/lib/smash_the_state/operation/state.rb +14 -14
- data/lib/smash_the_state/operation/step.rb +0 -4
- data/lib/smash_the_state/operation.rb +3 -3
- data/lib/smash_the_state/version.rb +1 -1
- data/spec/unit/operation/sequence_spec.rb +38 -30
- data/spec/unit/operation/state_spec.rb +4 -6
- data/spec/unit/operation_spec.rb +13 -15
- metadata +10 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9538187d0adbf668fd4cd35bfb97e04591c6d70c758109486251cf7ac66df1d7
|
4
|
+
data.tar.gz: 99eac2a8a14d9d30884b07171dab596ed8fd6ad662127cb3678e32a3a3041d3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cd363ad82626e3874e94c2c1a58fe41e6b44ce8cdb5260975f1e2132b3fde84cd4b5fdbd994ee374aeaed65dc40bca9c6f6a8bd4fb9f32829b58024657c6f5f
|
7
|
+
data.tar.gz: d23d8b91270cfdb27eb32e5faad64776c7da6f2b894d65a7b18e9ca7b6c04e87907555ff86b9b7666032a22cf0ad85452c4dffc709a0fc0d42e2f841259d667a
|
data/README.md
CHANGED
@@ -127,7 +127,7 @@ Let's say you have two database types: `WhiskeyDB` and `AbsintheDB`, each of whi
|
|
127
127
|
|
128
128
|
``` ruby
|
129
129
|
class WhiskeyDBCreateMiddleware
|
130
|
-
def create_environment(state)
|
130
|
+
def self.create_environment(state)
|
131
131
|
# do whiskey things
|
132
132
|
end
|
133
133
|
end
|
@@ -136,7 +136,7 @@ end
|
|
136
136
|
|
137
137
|
``` ruby
|
138
138
|
class AbsintheDBCreateMiddleware
|
139
|
-
def create_environment(state)
|
139
|
+
def self.create_environment(state)
|
140
140
|
# do absinthe things
|
141
141
|
end
|
142
142
|
end
|
@@ -93,13 +93,20 @@ module SmashTheState
|
|
93
93
|
step.error_handler = block
|
94
94
|
end
|
95
95
|
|
96
|
-
# rubocop:disable Lint/ShadowedException
|
97
96
|
def middleware_class(state, original_state = nil)
|
98
|
-
middleware_class_block.call(state, original_state)
|
99
|
-
|
100
|
-
|
97
|
+
klass = middleware_class_block.call(state, original_state)
|
98
|
+
|
99
|
+
case klass
|
100
|
+
when Module, Class
|
101
|
+
klass
|
102
|
+
else
|
103
|
+
begin
|
104
|
+
klass.constantize if klass.respond_to?(:constantize)
|
105
|
+
rescue NameError
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
end
|
101
109
|
end
|
102
|
-
# rubocop:enable Lint/ShadowedException
|
103
110
|
|
104
111
|
def add_middleware_step(step_name, options = {})
|
105
112
|
step = Operation::Step.new step_name, options do |state, original_state|
|
@@ -18,8 +18,8 @@ module SmashTheState
|
|
18
18
|
attr_accessor :representer
|
19
19
|
|
20
20
|
def build(params = nil, &block)
|
21
|
-
Class.new(self)
|
22
|
-
|
21
|
+
Class.new(self) do
|
22
|
+
class_exec(params, &block)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -29,7 +29,7 @@ module SmashTheState
|
|
29
29
|
def schema(key, options = {}, &block)
|
30
30
|
attribute key,
|
31
31
|
:state_for_smashing,
|
32
|
-
options.merge(
|
32
|
+
**options.merge(
|
33
33
|
# allow for schemas to be provided inline *or* as a reference to a
|
34
34
|
# type definition
|
35
35
|
schema: attribute_options_to_ref_block(options) || block
|
@@ -39,19 +39,19 @@ module SmashTheState
|
|
39
39
|
# for ActiveModel states we will treat the block as a collection of ActiveModel
|
40
40
|
# validator directives
|
41
41
|
def eval_validation_directives_block(state, &block)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
# each validate block should be a "fresh start" and not interfere with the
|
43
|
+
# previous blocks
|
44
|
+
state.singleton_class.clear_validators!
|
45
|
+
state.singleton_class.class_eval(&block)
|
46
|
+
|
47
|
+
state.validate || invalid!(state)
|
48
|
+
state
|
49
49
|
end
|
50
50
|
|
51
51
|
def extend_validation_directives_block(state, &block)
|
52
|
-
state.
|
53
|
-
|
54
|
-
|
52
|
+
state.class_eval(&block)
|
53
|
+
|
54
|
+
state
|
55
55
|
end
|
56
56
|
|
57
57
|
# for non-ActiveModel states we will just evaluate the block as a validator
|
@@ -69,7 +69,7 @@ module SmashTheState
|
|
69
69
|
|
70
70
|
# if a reference to a definition is provided, use the reference schema block
|
71
71
|
def attribute_options_to_ref_block(options)
|
72
|
-
options[:ref]
|
72
|
+
options[:ref]&.schema_block
|
73
73
|
end
|
74
74
|
|
75
75
|
def invalid!(state)
|
@@ -24,7 +24,7 @@ module SmashTheState
|
|
24
24
|
# inheritance doesn't work with class attr_readers, this method is provided to
|
25
25
|
# bootstrap an operation as a continuation of a "prelude" operation
|
26
26
|
def continues_from(prelude)
|
27
|
-
@state_class = prelude.state_class
|
27
|
+
@state_class = prelude.state_class&.dup
|
28
28
|
sequence.steps.concat prelude.sequence.steps
|
29
29
|
|
30
30
|
# also make the dry run sequence continue
|
@@ -52,7 +52,7 @@ module SmashTheState
|
|
52
52
|
sequence.override_step(step_name, options, &block)
|
53
53
|
|
54
54
|
# also override the dry run step
|
55
|
-
return if dry_run_sequence.steps_for_name(step_name).
|
55
|
+
return if dry_run_sequence.steps_for_name(step_name).empty?
|
56
56
|
|
57
57
|
dry_run_sequence.override_step(step_name, options, &block)
|
58
58
|
end
|
@@ -122,7 +122,7 @@ module SmashTheState
|
|
122
122
|
# state class can be nil if the schema is never defined. that's ok. in that
|
123
123
|
# situation it's up to the first step to produce the original state and we'll pass
|
124
124
|
# the params themselves in
|
125
|
-
state = state_class
|
125
|
+
state = state_class&.new(params)
|
126
126
|
sequence_to_run.call(state || params)
|
127
127
|
end
|
128
128
|
end
|
@@ -78,12 +78,10 @@ describe SmashTheState::Operation::Sequence do
|
|
78
78
|
context "without an error handler" do
|
79
79
|
it "re-raises the Error exception" do
|
80
80
|
expect do
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
expect(e.state).to eq([:foo])
|
86
|
-
end
|
81
|
+
instance.call([])
|
82
|
+
raise "should not hit this"
|
83
|
+
rescue SmashTheState::Operation::Error => e
|
84
|
+
expect(e.state).to eq([:foo])
|
87
85
|
end
|
88
86
|
end
|
89
87
|
end
|
@@ -156,16 +154,14 @@ describe SmashTheState::Operation::Sequence do
|
|
156
154
|
|
157
155
|
context "when no step by the given name exists" do
|
158
156
|
it "raises an exception" do
|
159
|
-
|
160
|
-
instance.override_step :four do
|
161
|
-
end
|
162
|
-
|
163
|
-
raise "should not reach this"
|
164
|
-
rescue SmashTheState::Operation::Sequence::BadOverride => e
|
165
|
-
expect(
|
166
|
-
e.to_s
|
167
|
-
).to eq("overriding step :four failed because it does not exist")
|
157
|
+
instance.override_step :four do
|
168
158
|
end
|
159
|
+
|
160
|
+
raise "should not reach this"
|
161
|
+
rescue SmashTheState::Operation::Sequence::BadOverride => e
|
162
|
+
expect(
|
163
|
+
e.to_s
|
164
|
+
).to eq("overriding step :four failed because it does not exist")
|
169
165
|
end
|
170
166
|
end
|
171
167
|
|
@@ -204,28 +200,40 @@ describe SmashTheState::Operation::Sequence do
|
|
204
200
|
end
|
205
201
|
|
206
202
|
context "with a middleware_class_block" do
|
207
|
-
|
208
|
-
|
203
|
+
context "that returns a string" do
|
204
|
+
it "runs the middleware_class_block, passes in the state, constantizes" do
|
205
|
+
expect(instance.middleware_class("String")).to eq(String)
|
206
|
+
end
|
209
207
|
end
|
210
|
-
end
|
211
208
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
209
|
+
context "that returns a constant" do
|
210
|
+
before do
|
211
|
+
instance.middleware_class_block = proc do |state|
|
212
|
+
state
|
213
|
+
end
|
214
|
+
end
|
217
215
|
|
218
|
-
|
219
|
-
|
216
|
+
context "that is a class" do
|
217
|
+
let!(:klass) { Class.new }
|
220
218
|
|
221
|
-
|
222
|
-
|
223
|
-
|
219
|
+
it "runs the middleware_class_block, passes in the state" do
|
220
|
+
expect(instance.middleware_class(klass)).to eq(klass)
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
context "that returns a module" do
|
225
|
+
let!(:modyule) { Module.new }
|
226
|
+
|
227
|
+
it "runs the middleware_class_block, passes in the state" do
|
228
|
+
expect(instance.middleware_class(modyule)).to eq(modyule)
|
229
|
+
end
|
224
230
|
end
|
225
231
|
end
|
232
|
+
end
|
226
233
|
|
234
|
+
context "with a NameError" do
|
227
235
|
it "returns nil" do
|
228
|
-
expect(instance.middleware_class(
|
236
|
+
expect(instance.middleware_class("Whazzat")).to eq(nil)
|
229
237
|
end
|
230
238
|
end
|
231
239
|
end
|
@@ -254,7 +262,7 @@ describe SmashTheState::Operation::Sequence do
|
|
254
262
|
instance.add_middleware_step :extra_step
|
255
263
|
end
|
256
264
|
|
257
|
-
it "block receives
|
265
|
+
it "block receives the state and original state" do
|
258
266
|
instance.middleware_class_block = proc do |state, original_state|
|
259
267
|
expect(state).to eq(baz: "bing")
|
260
268
|
expect(original_state).to eq(foo: "bar")
|
@@ -112,13 +112,11 @@ describe SmashTheState::Operation::State do
|
|
112
112
|
let!(:instance) { built.new }
|
113
113
|
|
114
114
|
it "calls the block" do
|
115
|
-
|
116
|
-
|
117
|
-
i.errors.add(:bread, "is moldy")
|
118
|
-
end
|
119
|
-
rescue SmashTheState::Operation::State::Invalid => e
|
120
|
-
expect(e.state.errors[:bread]).to include("is moldy")
|
115
|
+
SmashTheState::Operation::State.eval_custom_validator_block(instance) do |i|
|
116
|
+
i.errors.add(:bread, "is moldy")
|
121
117
|
end
|
118
|
+
rescue SmashTheState::Operation::State::Invalid => e
|
119
|
+
expect(e.state.errors[:bread]).to include("is moldy")
|
122
120
|
end
|
123
121
|
|
124
122
|
it "only raises an error for the current state" do
|
data/spec/unit/operation_spec.rb
CHANGED
@@ -237,17 +237,17 @@ describe SmashTheState::Operation do
|
|
237
237
|
context "with a custom dry run sequence block" do
|
238
238
|
before do
|
239
239
|
klass.step :step_one do |state, _original_state, run_options|
|
240
|
-
state.name = state.name
|
240
|
+
state.name = "#{state.name} one #{run_options.to_json}"
|
241
241
|
state
|
242
242
|
end
|
243
243
|
|
244
244
|
klass.step :step_two do |state|
|
245
|
-
state.name = state.name
|
245
|
+
state.name = "#{state.name} two"
|
246
246
|
state
|
247
247
|
end
|
248
248
|
|
249
249
|
klass.step :step_three do |state|
|
250
|
-
state.name = state.name
|
250
|
+
state.name = "#{state.name} three"
|
251
251
|
state
|
252
252
|
end
|
253
253
|
|
@@ -257,7 +257,7 @@ describe SmashTheState::Operation do
|
|
257
257
|
|
258
258
|
# we'll provide a custom implementation of this step
|
259
259
|
step :step_two do |state|
|
260
|
-
state.name = state.name
|
260
|
+
state.name = "#{state.name} custom"
|
261
261
|
state
|
262
262
|
end
|
263
263
|
|
@@ -341,7 +341,7 @@ describe SmashTheState::Operation do
|
|
341
341
|
|
342
342
|
context "when the policy permits" do
|
343
343
|
it "newifies the policy class with the state, runs the method" do
|
344
|
-
state = klass.call(current_user:
|
344
|
+
state = klass.call(current_user:)
|
345
345
|
expect(state.name).to eq("allowed")
|
346
346
|
end
|
347
347
|
end
|
@@ -352,13 +352,11 @@ describe SmashTheState::Operation do
|
|
352
352
|
end
|
353
353
|
|
354
354
|
it "raises an exception, embeds the policy instance" do
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
expect(e.policy_instance.user).to eq(current_user)
|
361
|
-
end
|
355
|
+
klass.call(current_user:)
|
356
|
+
raise "should not hit this"
|
357
|
+
rescue SmashTheState::Operation::NotAuthorized => e
|
358
|
+
expect(e.policy_instance).to be_a(@policy_klass)
|
359
|
+
expect(e.policy_instance.user).to eq(current_user)
|
362
360
|
end
|
363
361
|
end
|
364
362
|
end
|
@@ -434,7 +432,7 @@ describe SmashTheState::Operation do
|
|
434
432
|
context "with a validation step" do
|
435
433
|
before do
|
436
434
|
klass.step :run_this do |state|
|
437
|
-
state.name = state.name
|
435
|
+
state.name = "#{state.name} People"
|
438
436
|
state
|
439
437
|
end
|
440
438
|
|
@@ -448,7 +446,7 @@ describe SmashTheState::Operation do
|
|
448
446
|
end
|
449
447
|
|
450
448
|
klass.step :safe, side_effect_free: true do |state|
|
451
|
-
state.name = state.name
|
449
|
+
state.name = "#{state.name} are nice"
|
452
450
|
state
|
453
451
|
end
|
454
452
|
end
|
@@ -468,7 +466,7 @@ describe SmashTheState::Operation do
|
|
468
466
|
context "with no validation step" do
|
469
467
|
before do
|
470
468
|
klass.step :run_this, side_effect_free: true do |state|
|
471
|
-
state.name = state.name
|
469
|
+
state.name = "#{state.name} People"
|
472
470
|
state
|
473
471
|
end
|
474
472
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smash_the_state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dan Connor
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_attributes
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.2
|
19
|
+
version: '1.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.2
|
26
|
+
version: '1.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 6.0.
|
33
|
+
version: 6.0.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 6.0.
|
40
|
+
version: 6.0.0
|
41
41
|
description: ''
|
42
42
|
email:
|
43
43
|
- dan@danconnor.com
|
@@ -68,7 +68,7 @@ homepage: https://github.com/ibm-cloud/smash-the-state
|
|
68
68
|
licenses:
|
69
69
|
- Apache-2.0
|
70
70
|
metadata: {}
|
71
|
-
post_install_message:
|
71
|
+
post_install_message:
|
72
72
|
rdoc_options: []
|
73
73
|
require_paths:
|
74
74
|
- lib
|
@@ -83,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
85
|
requirements: []
|
86
|
-
rubygems_version: 3.
|
87
|
-
signing_key:
|
86
|
+
rubygems_version: 3.3.26
|
87
|
+
signing_key:
|
88
88
|
specification_version: 4
|
89
89
|
summary: A useful utility for transforming state that provides step sequencing, middleware,
|
90
90
|
and validation.
|