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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor


60
61
62
# File 'lib/celluloid/fsm.rb', line 60

def actor
  @actor
end

#stateObject (readonly)

Obtain the current state of the FSM


71
72
73
# File 'lib/celluloid/fsm.rb', line 71

def state
  @state
end

Class Method Details

.included(klass) ⇒ Object

Included hook to extend class methods


21
22
23
# File 'lib/celluloid/fsm.rb', line 21

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


75
76
77
# File 'lib/celluloid/fsm.rb', line 75

def attach(actor)
  @actor = actor
end

#initialize(actor = nil) ⇒ Object

Be kind and call super if you must redefine initialize


63
64
65
66
67
68
# File 'lib/celluloid/fsm.rb', line 63

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


87
88
89
90
91
92
93
94
95
96
# File 'lib/celluloid/fsm.rb', line 87

def transition(state_name, options = {})
  new_state = validate_and_sanitize_new_state(state_name)
  return unless new_state

  if handle_delayed_transitions(new_state, options[: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!”


99
100
101
# File 'lib/celluloid/fsm.rb', line 99

def transition!(state_name)
  @state = state_name
end