Module: Copland::InterceptorChainBuilder
- Defined in:
- lib/copland/interceptor-chain.rb
Overview
This module encapsulates the functionality for building interceptor chains.
Defined Under Namespace
Classes: InterceptedServiceProxy, InterceptorChainElement, InvocationContext, ProxyObjectChainElement
Class Method Summary collapse
-
.build(service, interceptors) ⇒ Object
This will apply the given interceptors to the given service by first ordering the interceptors based on their before and after preferences, and then dynamically modifying the service’s methods so that the chain of interceptors sits in front of each of them.
Class Method Details
.build(service, interceptors) ⇒ Object
This will apply the given interceptors to the given service by first ordering the interceptors based on their before and after preferences, 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.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/copland/interceptor-chain.rb', line 119 def build( service, interceptors ) return service if interceptors.empty? ordered_list = Orderer.order( interceptors ) chain = ProxyObjectChainElement.new( service ) ordered_list.reverse.each do |interceptor| element = InterceptorChainElement.new( interceptor.instantiate ) 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 =~ /^__/ singleton.class_eval <<-EOF def #{method}( *args, &block ) context = InvocationContext.new( :#{method}, args, block ) @chain.process_next( context ) end EOF end # allow the interceptor to intercept methods not explicitly # declared on the reciever. singleton.class_eval <<-EOF def method_missing( sym, *args, &block ) context = InvocationContext.new( sym, args, block ) @chain.process_next( context ) end EOF return service end |