README.md in end_state-0.10.1 vs README.md in end_state-0.11.0
- old
+ new
@@ -49,19 +49,21 @@
machine = Machine.new(StatefulObject.new(:a))
machine.transition :b # => true
machine.state # => :b
machine.b? # => true
-machine.c! # => true
+machine.transition :c # => true
machine.state # => :c
machine.can_transition? :b # => false
machine.can_transition? :a # => true
-machine.b! # => false
-machine.a! # => true
+machine.transition :b # => false
+machine.transition! :b # => raises InvalidTransition
+machine.transition :a # => true
machine.state # => :a
-machine.go! # => :true
+machine.go # => true
machine.state # => :b
+machine.go! # => raises InvalidTransition
```
## Initial State
If you wrap an object that currently has `nil` as the state, the state will be set to `:__nil__`.
@@ -84,17 +86,17 @@
transition b: :c
transition any_state: :d
end
machine = Machine.new(StatefulObject.new(:a))
-machine.d! # true
-machine.state # :d
+machine.transition :d # true
+machine.state # :d
machine = Machine.new(StatefulObject.new(:a))
-machine.b! # true
-machine.d! # true
-machine.state # :d
+machine.transition :b # true
+machine.transition :d # true
+machine.state # :d
```
## Guards
Guards can be created by subclassing `EndState::Guard`. Your class will be provided access to:
@@ -232,26 +234,28 @@
## Events
By using the `as` option in a transition definition you are creating an event representing that transition.
This can allow you to exercise the machine in a more natural "verb" style interaction. When using `as` event
definitions you can optionally set a `blocked` message on the transition. When the event is executed, if the
-machine is not in a state maching the initial state of the event, the message is added to the `failure_messages`
-array on the machine.
+machine is not in the initial state of the event, the message is added to the `failure_messages`
+array on the machine. Events, like `transition` have both a standard and a bang (`!`) style. The bang style
+will raise an exception if there is a problem.
-```
+```ruby
class Machine < EndState::StateMachine
transition a: :b, as: :go do |t|
t.blocked 'Cannot go!'
end
end
machine = Machine.new(StatefulObject.new(:a))
-machine.go! # => true
+machine.go # => true
machine.state # => :b
-machine.go! # => false
+machine.go # => false
machine.failure_messages # => ['Cannot go!']
+machine.go! # => raises InvalidTransition
```
## Parameters
When calling a transition, you can optionally provide a hash of parameters which will be available to the guards
@@ -259,28 +263,31 @@
When defining a transition you can indicate what parameters you are expecting with `allow_params` and `require_params`.
If you require any params then attempting to transition without them provided will raise an error. Specifying allowed
params is purely for documentation purposes.
-```
+```ruby
class Machine < EndState::StateMachine
- transition a: :b do |t|
+ transition a: :b, as: :go do |t|
t.allow_params :foo, :bar
end
end
```
-```
+```ruby
class Machine < EndState::StateMachine
- transition a: :b do |t|
+ transition a: :b, as: :go do |t|
t.require_params :foo, :bar
end
end
machine = Machine.new(StatefulObject.new(:a))
+machine.transition :b # => error raised: 'Missing params: foo, bar'
+machine.transition :b, foo: 1, bar: 'value' # => true
-machine.b! # => error raised: 'Missing params: foo, bar'
-machine.b! foo: 1, bar: 'value' # => true
+machine = Machine.new(StatefulObject.new(:a))
+machine.go # => error raised: 'Missing params: foo, bar'
+machine.go foo: 1, bar: 'value' # => true
```
## State storage
You may want to use an attribute other than `state` to track the state of the machine.