Module: Statemachine::StateBuilding

Included in:
StateBuilder, SuperstateBuilder
Defined in:
lib/statemachine/builder.rb

Overview

The builder module used to declare states.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



58
59
60
# File 'lib/statemachine/builder.rb', line 58

def subject
  @subject
end

Instance Method Details

#default(destination_id, action = nil) ⇒ Object

Declare a default transition for the state. Any event that is not already handled by the state will be handled by this transition.

sm = Statemachine.build do
  state :locked do
    default :unlock, :action
  end
end


115
116
117
# File 'lib/statemachine/builder.rb', line 115

def default(destination_id, action = nil)
  @subject.default_transition = Transition.new(@subject.id, destination_id, nil, action)
end

#event(event, destination_id, action = nil) ⇒ Object

Declares that the state responds to the specified event. The event parameter should be a Symbol.

The destination_id, which should also be a Symbol, is the id of the state that will event will transition into.

The 3rd action parameter is optional. Note that the action runs before the transition to the state specified in the 2nd parameter.

sm = Statemachine.build do
  state :locked do
    event :coin, :unlocked, :unlock
  end
end


74
75
76
# File 'lib/statemachine/builder.rb', line 74

def event(event, destination_id, action = nil)
  @subject.add(Transition.new(@subject.id, destination_id, event, action))
end

#on_entry(entry_action) ⇒ Object

Declare the entry action for the state.

sm = Statemachine.build do
  state :locked do
    on_entry :lock
  end
end


90
91
92
# File 'lib/statemachine/builder.rb', line 90

def on_entry(entry_action)
  @subject.entry_action = entry_action
end

#on_event(event, options) ⇒ Object



78
79
80
# File 'lib/statemachine/builder.rb', line 78

def on_event(event, options)
  self.event(event, options[:transition_to], options[:and_perform])
end

#on_exit(exit_action) ⇒ Object

Declare the exit action for the state.

sm = Statemachine.build do
  state :locked do
    on_exit :unlock
  end
end


102
103
104
# File 'lib/statemachine/builder.rb', line 102

def on_exit(exit_action)
  @subject.exit_action = exit_action
end