Module: Statemachine::SuperstateBuilding

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

Overview

The builder module used to declare superstates.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subjectObject (readonly)

Returns the value of attribute subject.



122
123
124
# File 'lib/statemachine/builder.rb', line 122

def subject
  @subject
end

Instance Method Details

#default_history(id) ⇒ Object

Used to specify the default state held by the history pseudo state of the superstate.

sm = Statemachine.build do
  superstate :operational do
    default_history :state_id
  end
end


216
217
218
# File 'lib/statemachine/builder.rb', line 216

def default_history(id)
  @subject.default_history = id
end

#on_entry_of(id, action) ⇒ Object

Allows the declaration of entry actions without using the state method. id is identifies the state to which the entry action will be added.

sm = Statemachine.build do
  trans :locked, :coin, :unlocked
  on_entry_of :unlocked, :unlock
end


192
193
194
# File 'lib/statemachine/builder.rb', line 192

def on_entry_of(id, action)
  @statemachine.get_state(id).entry_action = action
end

#on_exit_of(id, action) ⇒ Object

Allows the declaration of exit actions without using the state method. id is identifies the state to which the exit action will be added.

sm = Statemachine.build do
  trans :locked, :coin, :unlocked
  on_exit_of :locked, :unlock
end


204
205
206
# File 'lib/statemachine/builder.rb', line 204

def on_exit_of(id, action)
  @statemachine.get_state(id).exit_action = action
end

#startstate(startstate_id) ⇒ Object

Specifies the startstate for the statemachine or superstate. The state must exist within the scope.

sm = Statemachine.build do

startstate :locked

end



180
181
182
# File 'lib/statemachine/builder.rb', line 180

def startstate(startstate_id)
  @subject.startstate_id = startstate_id
end

#state(id, &block) ⇒ Object

Define a state within the statemachine or superstate.

sm = Statemachine.build do
  state :locked do
    #define the state
  end
end


132
133
134
135
# File 'lib/statemachine/builder.rb', line 132

def state(id, &block)
  builder = StateBuilder.new(id, @subject, @statemachine)
  builder.instance_eval(&block) if block
end

#superstate(id, &block) ⇒ Object

Define a superstate within the statemachine or superstate.

sm = Statemachine.build do
  superstate :operational do
    #define superstate
  end
end


145
146
147
148
# File 'lib/statemachine/builder.rb', line 145

def superstate(id, &block)
  builder = SuperstateBuilder.new(id, @subject, @statemachine)
  builder.instance_eval(&block)
end

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

Declares a transition within the superstate or statemachine.

The origin_id, a Symbol, identifies the starting state for this transition. The state identified by origin_id will be created within the statemachine or superstate which this transition is declared.

The event paramter 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. This method will not create destination states within the current statemachine of superstate. If the state destination state should exist here, that declare with with the state method or declare a transition starting at the state.

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


164
165
166
167
# File 'lib/statemachine/builder.rb', line 164

def trans(origin_id, event, destination_id, action = nil)
  origin = acquire_state_in(origin_id, @subject)
  origin.add(Transition.new(origin_id, destination_id, event, action))
end

#transition_from(origin_id, options) ⇒ Object



169
170
171
# File 'lib/statemachine/builder.rb', line 169

def transition_from(origin_id, options)
  trans(origin_id, options[:on_event], options[:transition_to], options[:and_perform])
end