Module: Stately::InstanceMethods

Defined in:
lib/stately.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Sets up an object with Stately. The DSL is parsed and the Stately::Machine is initialized.

When an object is first initialized, Stately automatically sets the state attribute to the start state.

Additionally, a method is defined for each of the state’s actions. These methods are used to transition between states. If you have a state named ‘completed’, Stately will infer the action to be ‘complete’ and define a method named ‘complete’. You can then call ‘complete’ on the object to transition into the completed state.



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

def InstanceMethods.included(klass)
  klass.class_eval do
    alias_method :init_instance, :initialize
    def initialize(*args)
      init_instance(*args)
      initialize_stately
    end

    stately_machine.states.each do |state|
      define_method(state.action) do
        transition_to(state)
      end

      define_method(:"#{state.name}?") do
        send(stately_machine.state_attr) == state.name.to_s
      end
    end
  end
end

Instance Method Details

#statesArray<String>

Returns a list of state names.

Returns:

  • (Array<String>)

    a list of state names.



105
106
107
# File 'lib/stately.rb', line 105

def states
  stately_machine.states.map(&:name)
end