Class: Stately::Machine

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

Overview

A Stately::Machine is a container for Stately::States.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, start) ⇒ Machine

Sets up a new instance of Stately::Machine



7
8
9
10
11
# File 'lib/stately/machine.rb', line 7

def initialize(attr_name, start)
  @state_attr = attr_name
  @start = start
  @states = [State.new(@start)]
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



4
5
6
# File 'lib/stately/machine.rb', line 4

def start
  @start
end

#state_attrObject (readonly)

Returns the value of attribute state_attr.



4
5
6
# File 'lib/stately/machine.rb', line 4

def state_attr
  @state_attr
end

#statesObject (readonly)

Returns the value of attribute states.



4
5
6
# File 'lib/stately/machine.rb', line 4

def states
  @states
end

Instance Method Details

#state(name, opts = {}, &block) ⇒ Object

Define a new Stately::State and add it to this Stately::Machine.

Parameters:

  • name (String)

    The name of the state. This is also stored in the instance object’s state attribute.

  • opts (Hash) (defaults to: {})

    Optionally, a method name can be defined as this state’s action, if it can’t be inferred from the name.



19
20
21
22
23
24
# File 'lib/stately/machine.rb', line 19

def state(name, opts={}, &block)
  @states.delete_if { |s| s.name == name }

  action = opts ? opts[:action] : nil
  @states << State.new(name, action, &block)
end