Module: ComposableStateMachine

Defined in:
lib/composable_state_machine.rb,
lib/composable_state_machine/model.rb,
lib/composable_state_machine/machine.rb,
lib/composable_state_machine/version.rb,
lib/composable_state_machine/behaviors.rb,
lib/composable_state_machine/callbacks.rb,
lib/composable_state_machine/transitions.rb,
lib/composable_state_machine/invalid_event.rb,
lib/composable_state_machine/callback_runner.rb,
lib/composable_state_machine/invalid_trigger.rb,
lib/composable_state_machine/invalid_transition.rb,
lib/composable_state_machine/default_callback_runner.rb,
lib/composable_state_machine/machine_with_external_state.rb

Overview

For examples, see the README.

Defined Under Namespace

Modules: CallbackRunner Classes: Behaviors, Callbacks, DefaultCallbackRunner, InvalidEvent, InvalidTransition, InvalidTrigger, Machine, MachineWithExternalState, Model, Transitions

Constant Summary collapse

VERSION =
'1.0.2'

Class Method Summary collapse

Class Method Details

.machine(model, *args) ⇒ Object

Creates a state machine from a model.

The variable number of arguments is driven by the difference in initialization APIs between different machines.

The last argument must be an options Hash. It may have at least the following options:

  • :callback_runner [Object] (DefaultCallbackRunner) Object whose #run_state_machine_callback method will be used to execute behavior callbacks. DefaultCallbackRunner simply calls a Proc’s #call method. When you want to execute callbacks in the context of an object, include the CallbackRunner mixin in the object and then pass the object as the callback runner here.

  • :state [Object] (model#initial_state) State the machine is in.

  • :model_factory [Object] (Machine) The object whose #new method will be called to create the machine.

Parameters:

  • model (Model, ...)

    the state machine model.

  • args (any)

    additional arguments passed to the model.



41
42
43
44
# File 'lib/composable_state_machine.rb', line 41

def self.machine(model, *args)
  options = args.last
  (options[:machine_factory] || Machine).new(model, *args)
end

.model(options) ⇒ Object

Creates a state machine model.

State machine models are immutable and can be shared across many state machine instances.

Parameters:

  • options (Hash)

    the options to create a model with.

Options Hash (options):

  • :transitions (Hash, Transitions)

    State machine transitions. A Transitions object will be created if a Hash is provided.

  • :behaviors (Hash, Behaviors)

    State machine behaviors. A Behaviors object will be created if a Hash is provided. If omitted, a high-performance behaviors stub will be used.

  • :callback_runner (Object) — default: DefaultCallbackRunner

    Object whose #run_state_machine_callback method will be used to execute behavior callbacks. DefaultCallbackRunner simply calls a Proc’s #call method.

  • :initial_state (Object) — default: nil

    Default initial state for the machine. This can be overriden by a machine instance.

  • :model_factory (Object) — default: Model

    The object whose #new method will be called to create a model.



18
19
20
21
22
23
24
25
26
27
# File 'lib/composable_state_machine.rb', line 18

def self.model(options)
  options = options.dup
  unless options[:transitions].respond_to?(:transition)
    options[:transitions] = Transitions.new(options[:transitions] || {})
  end
  unless options[:behaviors].respond_to?(:call)
    options[:behaviors] = Behaviors.new(options[:behaviors] || {})
  end
  (options[:model_factory] || Model).new(options)
end