end_state 0.10.0 → 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -0
- data/lib/end_state/state_machine.rb +2 -1
- data/lib/end_state/version.rb +1 -1
- data/spec/end_state/state_machine_spec.rb +11 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ab1a46deb255c511c400aa621f8115710df91b1
|
4
|
+
data.tar.gz: cf724f635788d18944aa6184c39d8a2eb99b5467
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a98ddb418c8ebd011a13d61d11132f325c16d00de89470199d849b80052f3c9a372766c9673d8b2649d5b545732c504358041846e45c4cdfcb61c63658208cc
|
7
|
+
data.tar.gz: d87346b6f8a48c9f2fb6d0e72a1f39b5216f2cf1af4ed23a42aa457bc47deb73d035c51648185cd9288244606ea626e9e2533baa0cd17364261a6aed803ac341
|
data/README.md
CHANGED
@@ -73,6 +73,28 @@ class Machine < EndState::StateMachine
|
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
76
|
+
## Special State - :any_state
|
77
|
+
|
78
|
+
You can specify the special state `:any_state` as the beginning of a transition. This will allow
|
79
|
+
the machine to transition to the new state specified from any actual state.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class Machine < EndState::StateMachine
|
83
|
+
transition a: :b
|
84
|
+
transition b: :c
|
85
|
+
transition any_state: :d
|
86
|
+
end
|
87
|
+
|
88
|
+
machine = Machine.new(StatefulObject.new(:a))
|
89
|
+
machine.d! # true
|
90
|
+
machine.state # :d
|
91
|
+
|
92
|
+
machine = Machine.new(StatefulObject.new(:a))
|
93
|
+
machine.b! # true
|
94
|
+
machine.d! # true
|
95
|
+
machine.state # :d
|
96
|
+
```
|
97
|
+
|
76
98
|
## Guards
|
77
99
|
|
78
100
|
Guards can be created by subclassing `EndState::Guard`. Your class will be provided access to:
|
@@ -230,6 +252,36 @@ machine.go! # => false
|
|
230
252
|
machine.failure_messages # => ['Cannot go!']
|
231
253
|
```
|
232
254
|
|
255
|
+
## Parameters
|
256
|
+
|
257
|
+
When calling a transition, you can optionally provide a hash of parameters which will be available to the guards
|
258
|
+
and concluders you include in the transition definition.
|
259
|
+
|
260
|
+
When defining a transition you can indicate what parameters you are expecting with `allow_params` and `require_params`.
|
261
|
+
If you require any params then attempting to transition without them provided will raise an error. Specifying allowed
|
262
|
+
params is purely for documentation purposes.
|
263
|
+
|
264
|
+
```
|
265
|
+
class Machine < EndState::StateMachine
|
266
|
+
transition a: :b do |t|
|
267
|
+
t.allow_params :foo, :bar
|
268
|
+
end
|
269
|
+
end
|
270
|
+
```
|
271
|
+
|
272
|
+
```
|
273
|
+
class Machine < EndState::StateMachine
|
274
|
+
transition a: :b do |t|
|
275
|
+
t.require_params :foo, :bar
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
machine = Machine.new(StatefulObject.new(:a))
|
280
|
+
|
281
|
+
machine.b! # => error raised: 'Missing params: foo, bar'
|
282
|
+
machine.b! foo: 1, bar: 'value' # => true
|
283
|
+
```
|
284
|
+
|
233
285
|
## State storage
|
234
286
|
|
235
287
|
You may want to use an attribute other than `state` to track the state of the machine.
|
@@ -128,7 +128,8 @@ module EndState
|
|
128
128
|
def state_for_event(event)
|
129
129
|
transitions = self.class.events[event]
|
130
130
|
return false unless transitions
|
131
|
-
|
131
|
+
start_states = transitions.map { |t| t.keys.first }
|
132
|
+
return invalid_event(event) unless start_states.include?(state.to_sym) || start_states.include?(:any_state)
|
132
133
|
transitions.first.values.first
|
133
134
|
end
|
134
135
|
|
data/lib/end_state/version.rb
CHANGED
@@ -221,6 +221,17 @@ module EndState
|
|
221
221
|
end
|
222
222
|
end
|
223
223
|
end
|
224
|
+
|
225
|
+
context 'when using any_state with an event' do
|
226
|
+
before do
|
227
|
+
StateMachine.transition any_state: :end, as: :jump_to_end
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'transitions the state to :end' do
|
231
|
+
machine.jump_to_end!
|
232
|
+
expect(machine.state).to eq :end
|
233
|
+
end
|
234
|
+
end
|
224
235
|
end
|
225
236
|
|
226
237
|
describe '#can_transition?' do
|