Module: DataMapper::Is::StateMachine::StateDsl

Defined in:
lib/dm-is-state_machine/is/dsl/state_dsl.rb

Overview

State DSL (Domain Specific Language)

Instance Method Summary collapse

Instance Method Details

#state(name, options = {}) ⇒ Object

Define a state of the system.

Example:

class TrafficLight
  include DataMapper::Resource
  property :id, Serial
  is :state_machine do
    state :green,  :enter => Proc.new { |o| o.log("G") }
    state :yellow, :enter => Proc.new { |o| o.log("Y") }
    state :red,    :enter => Proc.new { |o| o.log("R") }

    # event definitions go here...
  end

  def log(string)
    Merb::Logger.info(string)
  end
end


26
27
28
29
30
31
32
33
34
35
# File 'lib/dm-is-state_machine/is/dsl/state_dsl.rb', line 26

def state(name, options = {})
  unless state_machine_context?(:is)
    raise InvalidContext, "Valid only in 'is :state_machine' block"
  end

  # ===== Setup context =====
  machine = @is_state_machine[:machine]
  state = Data::State.new(name, machine, options)
  machine.states << state
end