Class: StateMachine::State

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-state-machine/state.rb

Defined Under Namespace

Classes: TransitionDefinitionDSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_machine, options) ⇒ StateMachine::State

Initializes a new State.

Examples:

StateMachine::State.new state_machine: my_fsm,
  :symbol => :doing_something,
  :name => "doing something very important"

Parameters:

  • state_machine (StateMachine)

    The state machine that the state belongs to.

  • options (Hash)

    Configuration options for the state.

Options Hash (options):

  • :symbol (Symbol)

    The state's identifier.

  • :name (String) — default: nil

    The state's name. Only used in debug log output (optional).


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/motion-state-machine/state.rb', line 59

def initialize(state_machine, options)
  @state_machine = state_machine
  @symbol = options[:symbol]
  @name = options[:name] || options[:symbol].to_s
  if @symbol.nil? || @state_machine.nil?
    raise ArgumentError, "Missing parameter"
  end

  @transition_map = {}
  @entry_actions = []
  @exit_actions = []
end

Instance Attribute Details

#entry_actionsArray

Returns an array of Proc objects called when entering the state.

Returns:

  • (Array)

    an array of Proc objects called when entering the state.


14
15
16
# File 'lib/motion-state-machine/state.rb', line 14

def entry_actions
  @entry_actions
end

#exit_actionsArray

Returns an array of Proc objects called when exiting the state.

Returns:

  • (Array)

    an array of Proc objects called when exiting the state.


18
19
20
# File 'lib/motion-state-machine/state.rb', line 18

def exit_actions
  @exit_actions
end

#nameString

Returns the state's name. Only used in debug log output.

Returns:

  • (String)

    the state's name. Only used in debug log output.


10
11
12
# File 'lib/motion-state-machine/state.rb', line 10

def name
  @name
end

#state_machineStateMachine::Base (readonly)

Returns the FSM that the state belongs to.

Returns:

  • (StateMachine::Base)

    the FSM that the state belongs to.


7
8
9
# File 'lib/motion-state-machine/state.rb', line 7

def state_machine
  @state_machine
end

#symbolSymbol (readonly)

Returns The state's identifying symbol.

Returns:

  • (Symbol)

    The state's identifying symbol.


4
5
6
# File 'lib/motion-state-machine/state.rb', line 4

def symbol
  @symbol
end

#transition_mapHash (readonly)

Returns The state machine's internal transition map (event types -> event values -> possible transitions).

Examples:

{
  :on => {
    :some_event => [transition1, transition2, ...],
    :other_event => [transition3, transition4, ...],
  },
  :after => {
    5.0 => [transition5, transition6, ...]
  }
}

Returns:

  • (Hash)

    The state machine's internal transition map (event types -> event values -> possible transitions)


35
36
37
# File 'lib/motion-state-machine/state.rb', line 35

def transition_map
  @transition_map
end

Instance Method Details

#register(transition) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers a transition in the transition map.

Parameters:

  • transition (Transition)

    the transition to register.


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/motion-state-machine/state.rb', line 88

def register(transition)
  event_type = transition.class.instance_variable_get(:@event_type)
  event_trigger_value = transition.event_trigger_value

  transition_map[event_type] ||= {}

  transitions =
    (transition_map[event_type][event_trigger_value] ||= [])
  transitions << transition

  transition
end

#terminating=(value) ⇒ Object


79
80
81
# File 'lib/motion-state-machine/state.rb', line 79

def terminating=(value)
  @terminating = !!value
end

#terminating?Boolean

Returns indicates if the state is a termination state.

Returns:

  • (Boolean)

    indicates if the state is a termination state.


75
76
77
# File 'lib/motion-state-machine/state.rb', line 75

def terminating?
  !!@terminating
end