Sha256: 313be4c988cbe7cfc5bd125c032e71c3fe98d8205aefb8d7cb7cd75d4efec30a

Contents?: true

Size: 929 Bytes

Versions: 13

Compression:

Stored size: 929 Bytes

Contents

module Spank
  class Proxy
    def initialize(target, interceptor_chain = InterceptorChain.new)
      @target = target
      @interceptor_chain = interceptor_chain
    end

    def add_interceptor(method, interceptor)
      @interceptor_chain.push(interceptor)
      self.extend(create_module_for(method))
      self
    end

    private

    def create_invocation_for(method, args, block)
      Invocation.new(@target, method, args, block)
    end

    def method_missing(method, *args, &block)
      if block
        @target.public_send(method, *args, block)
      else
        @target.public_send(method, *args)
      end
    end

    def create_module_for(method)
      Module.new do
        define_method(method.to_sym) do |*args, &block|
          invocation = create_invocation_for(method, args, block)
          @interceptor_chain.intercept(invocation)
          invocation.result
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
spank-1.1.0 lib/spank/proxy.rb
spank-1.0.1441140881 lib/spank/proxy.rb
spank-1.0.1441140857 lib/spank/proxy.rb
spank-1.0.1441140848 lib/spank/proxy.rb
spank-1.0.1441140841 lib/spank/proxy.rb
spank-1.0.1441140834 lib/spank/proxy.rb
spank-1.0.1441140824 lib/spank/proxy.rb
spank-1.0.1441140809 lib/spank/proxy.rb
spank-1.0.1441140804 lib/spank/proxy.rb
spank-1.0.1441140793 lib/spank/proxy.rb
spank-1.0.1420314444 lib/spank/proxy.rb
spank-0.0.1393558686 lib/spank/proxy.rb
spank-0.0.1385937112 lib/spank/proxy.rb