Class: SimplyFSM::StateMachine

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

Overview

The DSL for defining a state machine. These methods are used within the declaration of a state_machine.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eventsArray (readonly)

All the events of the state machine

Returns:

  • (Array)

    the current value of events



39
40
41
# File 'lib/simply_fsm/simply_fsm.rb', line 39

def events
  @events
end

#full_nameString (readonly)

The name of the owning class combined with the state machine’s name

Returns:

  • (String)

    the current value of full_name



39
40
41
# File 'lib/simply_fsm/simply_fsm.rb', line 39

def full_name
  @full_name
end

#initial_stateString (readonly)

The initial state of the state machine

Returns:

  • (String)

    the current value of initial_state



39
40
41
# File 'lib/simply_fsm/simply_fsm.rb', line 39

def initial_state
  @initial_state
end

#nameString (readonly)

Returns the current value of name.

Returns:

  • (String)

    the current value of name



39
40
41
# File 'lib/simply_fsm/simply_fsm.rb', line 39

def name
  @name
end

#statesArray (readonly)

All the states of the state machine

Returns:

  • (Array)

    the current value of states



39
40
41
# File 'lib/simply_fsm/simply_fsm.rb', line 39

def states
  @states
end

Instance Method Details

#event(event_name, transitions:, guard: nil, fail: nil) { ... } ⇒ Object

Define an event by event_name

Parameters:

  • event_name (String)
  • transitions (Hash, Array)

    either one (Hash) or many (Array of Hashes) transitions from one state to another state.

  • guard (Lambda) (defaults to: nil)

    if specified must return true before any transitions are attempted

  • fail (Lambda) (defaults to: nil)

    called with event name if specified when all the attempted transitions fail

Yields:

  • when the transition attempt succeeds.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simply_fsm/simply_fsm.rb', line 83

def event(event_name, transitions:, guard: nil, fail: nil, &after)
  return unless event_exists?(event_name) && transitions

  @events << event_name
  may_event_name = "may_#{event_name}?"

  if transitions.is_a?(Array)
    setup_multi_transition_may_event_method transitions: transitions, guard: guard,
                                            may_event_name: may_event_name
    setup_multi_transition_event_method event_name,
                                        transitions: transitions, guard: guard,
                                        var_name: "@#{@name}", fail: fail || @fail_handler
    return
  end

  to = transitions[:to]
  setup_may_event_method may_event_name, transitions[:from] || :any, transitions[:when], guard
  setup_event_method event_name, var_name: "@#{@name}",
                                 may_event_name: may_event_name, to: to,
                                 fail: fail || @fail_handler, &after
end

#state(state_name, initial: false) ⇒ Object

Declare a supported state_name, and optionally specify one as the initial state.

Parameters:

  • state_name (String)
  • initial (Boolean) (defaults to: false)

    to indicate if this is the initial state of the state machine



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simply_fsm/simply_fsm.rb', line 62

def state(state_name, initial: false)
  return if state_name.nil? || @states.include?(state_name)

  status = state_name.to_sym
  state_machine_name = @name
  @states << status
  @initial_state = status if initial

  make_owner_method "#{state_name}?", lambda {
    send(state_machine_name) == status
  }
end