### Home Classes Methods

### In Files

- lib/needle/interceptor-chain.rb

### Namespace

- CLASS Needle::InterceptorChainBuilder::InterceptedServiceProxy
- CLASS Needle::InterceptorChainBuilder::InterceptorChainElement
- CLASS Needle::InterceptorChainBuilder::ProxyObjectChainElement

### Methods

- ::build

### Files

- README

### Class/Module Index

Quicksearch

- Needle
- Needle::Bug
- Needle::Container
- Needle::DefinitionContext
- Needle::IncludeExclude
- Needle::Interceptor
- Needle::Interceptor::DynamicInterceptor
- Needle::InterceptorChainBuilder
- Needle::InterceptorChainBuilder::InterceptedServiceProxy
- Needle::InterceptorChainBuilder::InterceptorChainElement
- Needle::InterceptorChainBuilder::ProxyObjectChainElement
- Needle::InterceptorConfigurationError
- Needle::Lifecycle
- Needle::Lifecycle::Deferred
- Needle::Lifecycle::Initialize
- Needle::Lifecycle::Multiton
- Needle::Lifecycle::Proxy
- Needle::Lifecycle::Singleton
- Needle::Lifecycle::Threaded
- Needle::LogFactory
- Needle::Logger
- Needle::LoggingInterceptor
- Needle::NeedleError
- Needle::Pipeline
- Needle::Pipeline::Collection
- Needle::Pipeline::Collection::BlockElement
- Needle::Pipeline::Element
- Needle::Pipeline::InterceptorElement
- Needle::QueryableMutex
- Needle::Registry
- Needle::ServiceNotFound
- Needle::ServicePoint
- Needle::Version

No matching classes.

# Needle::InterceptorChainBuilder

This module encapsulates the functionality for building interceptor chains.

### Constants

InvocationContext

  The context of a method invocation. This is used in an interceptor chain to encapsulate the elements of the current invocation. sym: the name of the method being invoked args: the argument list being passed to the method block: the reference to the block attached to the method invocation data: a hash that may be used by clients for storing arbitrary data in

      the context.

### Public Class Methods

build( point, service, interceptors ) click to toggle source

This will apply the given interceptors to the given service by first ordering the interceptors based on their relative priorities, and then dynamically modifying the service’s methods so that the chain of interceptors sits in front of each of them.

The modified service is returned.

    # File lib/needle/interceptor-chain.rb, line 103
    def build( point, service, interceptors )
      return service if interceptors.nil? || interceptors.empty?

      ordered_list =
        interceptors.sort { |a,b|
          a.options[:priority] <=> b.options[:priority] }

      chain = ProxyObjectChainElement.new( service )

      ordered_list.reverse.each do |interceptor|
        factory = interceptor.action.call( point.container )
        instance = factory.new( point, interceptor.options )
        element = InterceptorChainElement.new( instance )
        element.next = chain
        chain = element
      end

      # FIXME: should inherited methods of "Object" be interceptable?
      methods_to_intercept = ( service.class.instance_methods( true ) -
                               Object.instance_methods +
                               service.class.instance_methods( false ) ).uniq

      service = InterceptedServiceProxy.new( chain )
      singleton = class << service; self; end

      methods_to_intercept.each do |method|
        next if method =~ /^__/

        if singleton.instance_methods(false).include? method
          singleton.send( :remove_method, method )
        end

        singleton.class_eval           def #{method}( *args, &block )            context = InvocationContext.new( :#{method}, args, block, Hash.new )            @chain.process_next( context )          end
      end

      # allow the interceptor to intercept methods not explicitly
      # declared on the reciever.
      if singleton.instance_methods(false).include? "method_missing"
        singleton.send( :remove_method, :method_missing )
      end

      singleton.class_eval         def method_missing( sym, *args, &block )          context = InvocationContext.new( sym, args, block, Hash.new )          @chain.process_next( context )        end

      return service
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.