Class: Statemachine::StatemachineBuilder

Inherits:
Builder
  • Object
show all
Includes:
SuperstateBuilding
Defined in:
lib/statemachine/builder.rb

Overview

Created by Statemachine.build as the root context for building the statemachine.

Instance Attribute Summary

Attributes included from SuperstateBuilding

#subject

Attributes inherited from Builder

#statemachine

Instance Method Summary collapse

Methods included from SuperstateBuilding

#default_history, #on_entry_of, #on_exit_of, #startstate, #state, #superstate, #trans, #transition_from

Constructor Details

#initialize(statemachine = Statemachine.new) ⇒ StatemachineBuilder

Returns a new instance of StatemachineBuilder.



248
249
250
251
# File 'lib/statemachine/builder.rb', line 248

def initialize(statemachine = Statemachine.new)
  super statemachine
  @subject = @statemachine.root
end

Instance Method Details

#context(a_context) ⇒ Object

Used the set the context of the statemahine within the builder.

sm = Statemachine.build do
  ...
  context MyContext.new
end

Statemachine.context may also be used.



261
262
263
264
# File 'lib/statemachine/builder.rb', line 261

def context(a_context)
  @statemachine.context = a_context
  a_context.statemachine = @statemachine if a_context.respond_to?(:statemachine=)
end

#on_state_change(action = nil, &block) ⇒ Object

This callback would be called whenever the statemachine is changing the state. This is useful whenever we want to make the state persistent and store into some database.



283
284
285
# File 'lib/statemachine/builder.rb', line 283

def on_state_change(action=nil, &block)
  @statemachine.state_change_action = action || block
end

#stub_context(options = {}) ⇒ Object

Stubs the context. This makes statemachine immediately useable, even if functionless. The stub will print all the actions called so it’s nice for trial runs.

sm = Statemachine.build do
  ...
  stub_context :verbose => true
end

Statemachine.context may also be used.



275
276
277
278
# File 'lib/statemachine/builder.rb', line 275

def stub_context(options={})
  require 'statemachine/stub_context'
  context StubContext.new(options)
end