Class: Compendium::ContextWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/compendium/context_wrapper.rb

Class Method Summary collapse

Class Method Details

.wrap(ctx, parent, params = nil, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/compendium/context_wrapper.rb', line 5

def self.wrap(ctx, parent, params = nil, &block)
  delegator = ::SimpleDelegator.new(parent)

  delegator.define_singleton_method(:__context__) { ctx }

  delegator.instance_eval do
    def method_missing(name, *args, &block)
      return __context__.__send__(name, *args, &block) if __context__.respond_to?(name)
      super
    end

    def respond_to_missing?(name, include_private = false)
      return true if __context__.respond_to?(name, include_private)
      super
    end
  end

  return delegator.instance_exec(params, &block) if block_given?

  delegator
end