Sha256: 379bd96ebdeb6bb214d2dd5a4b57daa32509ecde42aaba286f31e91a6c4c492a

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

class DslBlock

  # The Executor class is designed to run a block of code in isolation
  # for the DslBlock class. By running it in a 'sandbox', the block
  # of code cannot inadvertently access protected and private methods
  # within the DslBlock without explicate declaration by the DslBlock.
  # To the user, it will appear that the block runs directly in the
  # DslBlock but in a partly restricted manner if they care to investigate.
  # In this fashion, executor is effectively a transparent proxy.
  class Executor < BasicObject

    def initialize(dsl_block)
      @dsl_block = dsl_block
    end

    def method_missing(method, *args, &block)
      # If the dsl block lists the method as a callable command
      if @dsl_block._commands.include?(method)
        # Attempt to call it
        begin
          @dsl_block.send(method, *args, &block)
        rescue => e
          # If there is any type of error, remove ourselves from the callstack to reduce confusion.
          e.set_backtrace(::Kernel.caller.select { |x| !x.include?(__FILE__)})
          ::Kernel.raise e
        end
      else
        # Otherwise raise a no method error as if the method does not really exist, regardless of reality.
        name_error = ::NameError.new("undefined local variable or method `#{method}' for #{self.inspect}")
        name_error.set_backtrace(::Kernel.caller.select { |x| !x.include?(__FILE__)})
        ::Kernel::raise name_error
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dsl_block-1.0.0 lib/dsl_block/executor.rb