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
-
.machine(model, *args) ⇒ Object
Creates a state machine from a model.
-
.model(options) ⇒ Object
Creates a state machine model.
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.
41 42 43 44 |
# File 'lib/composable_state_machine.rb', line 41 def self.machine(model, *args) = args.last ([: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.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/composable_state_machine.rb', line 18 def self.model() = .dup unless [:transitions].respond_to?(:transition) [:transitions] = Transitions.new([:transitions] || {}) end unless [:behaviors].respond_to?(:call) [:behaviors] = Behaviors.new([:behaviors] || {}) end ([:model_factory] || Model).new() end |