Class: ComposableStateMachine::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/composable_state_machine/model.rb

Overview

An immutable state machine model that can be shared across many instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Model

Creates a state machine model.

Parameters:

  • options (Hash) (defaults to: {})

    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.

  • :initial_state (Object) — default: nil

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

  • :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.



15
16
17
18
19
20
# File 'lib/composable_state_machine/model.rb', line 15

def initialize(options = {})
  @initial_state = options[:initial_state]
  @transitions = options[:transitions]
  @behaviors = options[:behaviors] || proc {}
  @callback_runner = options[:callback_runner] || DefaultCallbackRunner
end

Instance Attribute Details

#callback_runnerObject (readonly)

Returns the value of attribute callback_runner.



6
7
8
# File 'lib/composable_state_machine/model.rb', line 6

def callback_runner
  @callback_runner
end

#initial_stateObject (readonly)

Returns the value of attribute initial_state.



5
6
7
# File 'lib/composable_state_machine/model.rb', line 5

def initial_state
  @initial_state
end

Instance Method Details

#run_callbacks(callback_runner, current_state, event, new_state, arguments, &block) ⇒ Object

Runs the callbacks for all behaviors for a state transition



44
45
46
47
# File 'lib/composable_state_machine/model.rb', line 44

def run_callbacks(callback_runner, current_state, event, new_state, arguments, &block)
  run_callbacks_for(callback_runner, :enter, new_state,
                    current_state, event, new_state, *arguments)
end

#run_callbacks_for(callback_runner, behavior, *args) ⇒ Object

Runs the callbacks for one behavior for a start transition



50
51
52
# File 'lib/composable_state_machine/model.rb', line 50

def run_callbacks_for(callback_runner, behavior, *args)
  @behaviors.call(callback_runner, behavior, *args)
end

#transition(current_state, event, arguments = [], callback_runner = nil) {|Object| ... } ⇒ Object?

Performs a transition of the machine, executing behaviors as needed.

Parameters:

  • current_state (Object)

    the current state of the machine

  • event (Object)

    the event the machine has received

  • arguments (Enumerable) (defaults to: [])

    ([]) any arguments related to the event.

  • callback_runner (Object) (defaults to: nil)

    (#callback_runner) the runner with which to execute callbacks

Yields:

  • (Object)

    the new state of the machine, if a transition happened

Returns:

  • (Object)

    the new state of the machine, if a transition happened

  • (nil)

    if no transition happened



33
34
35
36
37
38
39
40
41
# File 'lib/composable_state_machine/model.rb', line 33

def transition(current_state, event, arguments = [], callback_runner = nil)
  @transitions.transition(current_state, event).tap do |new_state|
    if new_state && new_state != current_state
      callback_runner ||= @callback_runner
      run_callbacks(callback_runner, current_state, event, new_state, arguments)
      yield new_state if block_given?
    end
  end
end