Parent

Files

Needle::Interceptor

This represents the definition of an interceptor as it is attached to a service point. Instances of Interceptor are also used for configuring themselves programmatically.

You will almost never instantiate an Interceptor object directly. Instead, use the Container#intercept method. You can then configure the new interceptor by chaining methods of the new object together, quite readably:

  container.intercept( :foo ).with! { some_interceptor }.
    with_options( :arg => :value )

You can also create new interceptors on the fly via the Interceptor#doing method.

Attributes

options[R]

The set of options that were given to this interceptor via the # method.

Public Class Methods

new() click to toggle source

Create a new Interceptor definition. By default, it has no implementation and a priority of 0.

    # File lib/needle/interceptor.rb, line 81
81:     def initialize
82:       @options = { :priority => 0 }
83:       @doing = @with = nil
84:     end

Public Instance Methods

[]( name ) click to toggle source

A convenience method for querying the options on an interceptor definition.

     # File lib/needle/interceptor.rb, line 177
177:     def []( name )
178:       @options[ name ]
179:     end
[]=( name, value ) click to toggle source

A convenience method for setting the options on an interceptor definition.

     # File lib/needle/interceptor.rb, line 183
183:     def []=( name, value )
184:       @options[ name ] = value
185:     end
action() click to toggle source

Returns the action that was specified for this interceptor as a proc instance. This will either be the block passed to #, or a proc that wraps the instantiation of a DynamicInterceptor (when # was used).

If neither # nor # were specified, an InterceptorConfigurationError is raised.

    # File lib/needle/interceptor.rb, line 93
93:     def action
94:       return @with if @with
95:       raise InterceptorConfigurationError,
96:         "You must specify either 'with' or 'doing'" unless @doing
97: 
98:       return proc { |c| DynamicInterceptor.new( @doing ) }
99:     end
doing( &block ) click to toggle source

This allows new interceptors to be defined “on-the-fly”. The associated block must accept two parameters—an object representing the chain of interceptors, and the context of the current method invocation. The block should then invoke # on the chain (passing the context as the lone parameter) when the next element of the chain should be invoked.

You should only call # once per interceptor, and never after invoking # on the same interceptor.

     # File lib/needle/interceptor.rb, line 149
149:     def doing( &block )
150:       if @doing
151:         raise InterceptorConfigurationError,
152:           "you cannot redefine 'doing' behavior"
153:       end
154: 
155:       if @with
156:         raise InterceptorConfigurationError,
157:           "cannot specify 'doing' after specifying 'with'"
158:       end
159: 
160:       if block.nil?
161:         raise InterceptorConfigurationError,
162:           "you must specify a block to 'doing'"
163:       end
164: 
165:       @doing = block
166:       self
167:     end
with( &block ) click to toggle source

Sets the action for this interceptor to be that defined by the interceptor returned when the block is executed. You can only invoke # once, and never after previously invoking # on the same interceptor instance.

Usage:

  container.intercept( :foo ).
    with { |c| c.logging_interceptor }
     # File lib/needle/interceptor.rb, line 110
110:     def with( &block )
111:       if @with
112:         raise InterceptorConfigurationError,
113:           "you cannot redefine 'with' behavior"
114:       end
115: 
116:       if @doing
117:         raise InterceptorConfigurationError,
118:           "cannot specify 'with' after specifying 'doing'"
119:       end
120: 
121:       if block.nil?
122:         raise InterceptorConfigurationError,
123:           "you must specify a block to 'with'"
124:       end
125: 
126:       @with = block
127:       self
128:     end
with!( &block ) click to toggle source

This is identical to #, but it wraps the block in another proc that calls instance_eval on the container, with the block.

Usage:

  container.intercept( :foo ).
    with! { logging_interceptor }
     # File lib/needle/interceptor.rb, line 137
137:     def with!( &block )
138:       with { |c| c.instance_eval( &block ) }
139:     end
with_options( opts={} ) click to toggle source

Merge the given opts hash into the interceptors options hash.

     # File lib/needle/interceptor.rb, line 170
170:     def with_options( opts={} )
171:       @options.update opts
172:       self
173:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.