Class: Golem::DSL::StateDef

Inherits:
Object
  • Object
show all
Defined in:
lib/golem/dsl/state_def.rb

Instance Method Summary collapse

Constructor Details

#initialize(machine, state_name, options = {}, &block) ⇒ StateDef

Returns a new instance of StateDef.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/golem/dsl/state_def.rb', line 12

def initialize(machine, state_name, options = {}, &block)
  @machine = machine
  @state = @machine.get_or_define_state(state_name)
  @options = options
  
  if options[:enter]
    @state.callbacks[:on_enter] = Golem::Model::Callback.new(options[:enter]) 
  end
  
  if options[:exit]
     @state.callbacks[:on_exit] = Golem::Model::Callback.new(options[:exit])
  end
  
  instance_eval(&block) if block
end

Instance Method Details

#enter(callback = nil, &block) ⇒ Object



32
33
34
35
36
# File 'lib/golem/dsl/state_def.rb', line 32

def enter(callback = nil, &block)
  raise Golem::DefinitionSyntaxError, "Enter action already set." if @state.callbacks[:on_enter]
  raise Golem::DefinitionSyntaxError, "Provide either a callback method or a block, not both." if callback && block
  @state.callbacks[:on_enter] = Golem::Model::Callback.new(block || callback)
end

#exit(callback = nil, &block) ⇒ Object



38
39
40
41
42
# File 'lib/golem/dsl/state_def.rb', line 38

def exit(callback = nil, &block)
  raise Golem::DefinitionSyntaxError, "Exit action already set." if @state.callbacks[:on_exit]
  raise Golem::DefinitionSyntaxError, "Provide either a callback method or a block, not both." if callback && block
  @state.callbacks[:on_exit] = Golem::Model::Callback.new(block || callback)
end

#on(event_name, options = {}, &block) ⇒ Object



28
29
30
# File 'lib/golem/dsl/state_def.rb', line 28

def on(event_name, options = {}, &block)
  Golem::DSL::EventDef.new(@machine, @state, event_name, options, &block)
end