very_tiny_state_machine

For when the others are not tiny enough.

The entire state machine lives in a separate variable, and does not pollute the class or the module of the caller. The state machine has the ability to dispatch callbacks when states are switched, and the callbacks are dispatched to the given object as messages.

@automaton = VeryTinyStateMachine.new(:initialized, self)
@automaton.permit_states_and_transitions(:initialized => :processing, :processing => :closing, :closing => [:closed])
@automaton.permit_state :failed
@automaton.permit_transition :closing => :failed

# Then, lower down the code
@automaton.transition! :processing

The object supplied as the optional second argument will receive messages when states are switched around, in the following order (using the state machine from the previous example):

# self.leaving_initialized_state
# self.entering_processing_state
# self.transitioning_from_initialized_to_processing_state
# ..the state variable is switched here
# self.after_transitioning_from_initialized_to_processing_state
# self.after_leaving_initialized_state
# self.after_entering_processing_state

You can see in which state the machine is in:

@automaton.in_state?(:processing) #=> true
@automaton.in_state?(:initialized) #=> false

and state machine has your back if you want to do something invalid:

@automaton.transition :initialized # Will raise TinyStateMachine::InvalidFlow
@automaton.transition :something_odd # Will raise TinyStateMachine::UnknownState

Contributing to very_tiny_state_machine

Copyright © 2016 WeTransfer. See LICENSE.txt for further details.