Class: Woyo::Action

Inherits:
WorldObject show all
Defined in:
lib/woyo/world/action.rb

Instance Attribute Summary

Attributes inherited from WorldObject

#_test, #context, #id

Instance Method Summary collapse

Methods inherited from WorldObject

#attribute_changes, #attribute_clear_changes, #changes, #clear_changes, #initialize, #uid

Methods included from Evaluate

#children, #evaluate, included

Methods included from Attributes

#attribute, #attributes, #changes, #clear_changes, #define_attr, #define_attr!, #define_attr?, #define_attr_default, #define_attr_equals, #define_attr_methods, #dependent_changes, #exclusion, #exclusions, #group, #groups, #is, #is?, #track_changes

Constructor Details

This class inherits a constructor from Woyo::WorldObject

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/woyo/world/action.rb', line 14

def execute
  location_or_context.clear_changes
  result = if @proc.arity < 1
    @context.instance_eval &@proc
  else
    @context.instance_exec self, &@proc
  end
  unless result.kind_of? Hash
    result = { return: result }
  end
  # result: { location: :place } signals change of location (like going a way!)
  # todo: fill return hash with action attributes, groups, exclusions ? ...
  { describe: describe, result: result, changes: location_or_context.changes }
end

#execution(&block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/woyo/world/action.rb', line 29

def execution &block
  if block_given?
    @proc = block
  else
    @proc
  end
end

#initialize_objectObject



8
9
10
11
12
# File 'lib/woyo/world/action.rb', line 8

def initialize_object
  super
  attribute :describe
  @proc = proc { nil }
end

#location_or_contextObject



37
38
39
40
41
42
43
44
# File 'lib/woyo/world/action.rb', line 37

def location_or_context
  ancestor = self 
  while ancestor.context do
    return ancestor if ancestor.kind_of? Woyo::Location
    ancestor = ancestor.context 
  end
  ancestor
end