Module: Celluloid::FSM
- Defined in:
- lib/celluloid/fsm.rb
Overview
Simple finite state machines with integrated Celluloid timeout support Inspired by Erlang’s gen_fsm (www.erlang.org/doc/man/gen_fsm.html)
Basic usage:
class MyMachine
include Celluloid::FSM # NOTE: this does NOT pull in the Celluloid module
end
Inside an actor:
#
machine = MyMachine.new(current_actor)
Defined Under Namespace
Modules: ClassMethods Classes: State, UnattachedError
Constant Summary collapse
- DEFAULT_STATE =
Default state name unless one is explicitly set
:default
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
-
#state ⇒ Object
readonly
Obtain the current state of the FSM.
Class Method Summary collapse
-
.included(klass) ⇒ Object
Included hook to extend class methods.
Instance Method Summary collapse
-
#attach(actor) ⇒ Object
(also: #actor=)
Attach this FSM to an actor.
-
#initialize(actor = nil) ⇒ Object
Be kind and call super if you must redefine initialize.
-
#transition(state_name, options = {}) ⇒ Object
Transition to another state Options: * delay: don’t transition immediately, wait the given number of seconds.
-
#transition!(state_name) ⇒ Object
Immediate state transition with no sanity checks, or callbacks.
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
62 63 64 |
# File 'lib/celluloid/fsm.rb', line 62 def actor @actor end |
#state ⇒ Object (readonly)
Obtain the current state of the FSM
73 74 75 |
# File 'lib/celluloid/fsm.rb', line 73 def state @state end |
Class Method Details
.included(klass) ⇒ Object
Included hook to extend class methods
23 24 25 |
# File 'lib/celluloid/fsm.rb', line 23 def self.included(klass) klass.send :extend, ClassMethods end |
Instance Method Details
#attach(actor) ⇒ Object Also known as: actor=
Attach this FSM to an actor. This allows FSMs to wait for and initiate events in the context of a particular actor
77 78 79 |
# File 'lib/celluloid/fsm.rb', line 77 def attach(actor) @actor = actor end |
#initialize(actor = nil) ⇒ Object
Be kind and call super if you must redefine initialize
65 66 67 68 69 70 |
# File 'lib/celluloid/fsm.rb', line 65 def initialize(actor = nil) @state = self.class.default_state @delayed_transition = nil @actor = actor @actor ||= Celluloid.current_actor if Celluloid.actor? end |
#transition(state_name, options = {}) ⇒ Object
Transition to another state Options:
-
delay: don’t transition immediately, wait the given number of seconds.
This will return a Celluloid::Timer object you can use to cancel the pending state transition.
Note: making additional state transitions will cancel delayed transitions
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/celluloid/fsm.rb', line 89 def transition(state_name, = {}) new_state = validate_and_sanitize_new_state(state_name) return unless new_state if handle_delayed_transitions(new_state, [:delay]) return @delayed_transition end transition_with_callbacks!(new_state) end |
#transition!(state_name) ⇒ Object
Immediate state transition with no sanity checks, or callbacks. “Dangerous!”
101 102 103 |
# File 'lib/celluloid/fsm.rb', line 101 def transition!(state_name) @state = state_name end |