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
27
28
29
30
# 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
  
  if options[:comment]
    @state.comment = options[:comment]
  end
  
  instance_eval(&block) if block
end

Instance Method Details

#comment(comment) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/golem/dsl/state_def.rb', line 48

def comment(comment)
  if @state.comment
    @state.comment += "\n#{comment}"
  else
    @state.comment = comment
  end
end

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



36
37
38
39
40
# File 'lib/golem/dsl/state_def.rb', line 36

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



42
43
44
45
46
# File 'lib/golem/dsl/state_def.rb', line 42

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



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

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