Sha256: ce39776ae2d2b1b8a069ed95902a2e75e99386d1934009c7f82beede5ad2e22e

Contents?: true

Size: 1000 Bytes

Versions: 1

Compression:

Stored size: 1000 Bytes

Contents

require_relative 'world_object'
require_relative 'location'

module Woyo

class Action < WorldObject

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

  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

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

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

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
woyo-world-0.0.9 lib/woyo/world/action.rb