Class: Stately::State

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

Overview

A Stately::State object contains the configuration and other information about a defined state.

It’s made up of a name (which is saved to the parent instance’s state attribute), the name of an action (which is a method called to transition into this state), and a DSL to define allowed transitions, callbacks, and validations.

Defined Under Namespace

Classes: StateConfigurator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, action = nil, &block) ⇒ State

Sets up and returns a new Stately::State object.

Parameters:

  • name (String)

    The name of the state

  • action (String) (defaults to: nil)

    The method name that’s called to transition to this state. Some method names can be inferred based on the state’s name.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stately/state.rb', line 19

def initialize(name, action=nil, &block)
  @action = (action || guess_action_for(name)).to_s
  @name = name

  @allow_from_states = []
  @prevent_from_states = []

  @before_transitions = []
  @after_transitions = []
  @validations = []

  if block_given?
    configuration = StateConfigurator.new(&block)

    @allow_from_states = configuration.allow_from_states || []
    @prevent_from_states = configuration.prevent_from_states || []

    @after_transitions = configuration.after_transitions || []
    @before_transitions = configuration.before_transitions || []
    @validations = configuration.validations || []
  end
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



10
11
12
# File 'lib/stately/state.rb', line 10

def action
  @action
end

#after_transitionsObject (readonly)

Returns the value of attribute after_transitions.



12
13
14
# File 'lib/stately/state.rb', line 12

def after_transitions
  @after_transitions
end

#allow_from_statesObject (readonly)

Returns the value of attribute allow_from_states.



11
12
13
# File 'lib/stately/state.rb', line 11

def allow_from_states
  @allow_from_states
end

#before_transitionsObject (readonly)

Returns the value of attribute before_transitions.



12
13
14
# File 'lib/stately/state.rb', line 12

def before_transitions
  @before_transitions
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/stately/state.rb', line 10

def name
  @name
end

#prevent_from_statesObject (readonly)

Returns the value of attribute prevent_from_states.



11
12
13
# File 'lib/stately/state.rb', line 11

def prevent_from_states
  @prevent_from_states
end

#validationsObject (readonly)

Returns the value of attribute validations.



12
13
14
# File 'lib/stately/state.rb', line 12

def validations
  @validations
end

Instance Method Details

#to_sString

Returns The state name as a string.

Returns:

  • (String)

    The state name as a string



43
44
45
# File 'lib/stately/state.rb', line 43

def to_s
  @name.to_s
end

#to_symSymbol

Returns The state name as a string.

Returns:

  • (Symbol)

    The state name as a string



48
49
50
# File 'lib/stately/state.rb', line 48

def to_sym
  @name.to_sym
end