end_state 0.10.0 → 0.10.1

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: c3fceb0c3ef5dd0efcf27176e899e75ad55f1243
4
- data.tar.gz: 600cc39bea6aeca957285707a280c29b07092ad1
3
+ metadata.gz: 4ab1a46deb255c511c400aa621f8115710df91b1
4
+ data.tar.gz: cf724f635788d18944aa6184c39d8a2eb99b5467
5
5
  SHA512:
6
- metadata.gz: 05317e5548b4924e211f422a2a9c4eb6f6ddcc872f4fdff91828acf27768cde90ec608f10c35d50b098576e797a6c2ec4ae9a8a010fd9046c09c0e4a01e282f5
7
- data.tar.gz: d3432693ec760a0ea06ba9790f075a3837571b0b53518b06f781ccaa9654832ff96b875eea229afb5ffe147fd1fbdc25bb9be93b4a6ea250552695c0cf3f7b2f
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
- return invalid_event(event) unless transitions.map { |t| t.keys.first }.include?(state.to_sym)
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
 
@@ -1,3 +1,3 @@
1
1
  module EndState
2
- VERSION = '0.10.0'
2
+ VERSION = '0.10.1'
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: end_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - alexpeachey