This class is used by the Container#define! and Container#namespace! methods to allow an instance_eval‘d block to create new service points simply by invoking imaginary methods. It is basically an empty shell, with almost all of the builtin methods removed from it. (This allows services like "hash" and "print" to be defined, where they would normally conflict with the Kernel methods of the same name.)
- has_key?
- intercept
- knows_key?
- method_missing
- namespace
- namespace!
- namespace_define
- namespace_define!
- new
- require
- this_container
- use
- use!
Create a new DefinitionContext that wraps the given container. All operations performed on this context will be delegated to the container.
[ show source ]
# File lib/needle/definition-context.rb, line 40 40: def initialize( container ) 41: @container = container 42: end
Delegates to Container#has_key?.
[ show source ]
# File lib/needle/definition-context.rb, line 102 102: def has_key?( name ) 103: @container.has_key?( name ) 104: end
Delegate to Container#intercept.
[ show source ]
# File lib/needle/definition-context.rb, line 51 51: def intercept( name ) 52: @container.intercept( name ) 53: end
Delegates to Container#knows_key?.
[ show source ]
# File lib/needle/definition-context.rb, line 107 107: def knows_key?( name ) 108: @container.knows_key?( name ) 109: end
Any method invocation with no block and no parameters is interpreted to be a service reference on the wrapped container, and delegates to Container#[]. If the block is not given but the args are not empty, a NoMethodError will be raised.
If a block is given, this delegates to Container#register, leaving all parameters in place.
[ show source ]
# File lib/needle/definition-context.rb, line 118 118: def method_missing( sym, *args, &block ) 119: if block.nil? 120: @container.get( sym, *args ) 121: else 122: @container.register( sym, *args, &block ) 123: end 124: end
Delegate to Container#namespace.
[ show source ]
# File lib/needle/definition-context.rb, line 56 56: def namespace( *parms, &block ) 57: @container.namespace( *parms, &block ) 58: end
Alias for namespace_define!
Delegate to Container#define on the new namespace.
[ show source ]
# File lib/needle/definition-context.rb, line 68 68: def namespace_define( *parms, &block ) 69: @container.namespace( *parms ) { |ns| ns.define( &block ) } 70: end
Delegate to Container#namespace_define!.
[ show source ]
# File lib/needle/definition-context.rb, line 61 61: def namespace_define!( *parms, &block ) 62: @container.namespace_define!( *parms, &block ) 63: end
Delegate to Container#require on the current container.
[ show source ]
# File lib/needle/definition-context.rb, line 73 73: def require( *parms ) 74: # this is necessary to work around an rdoc bug...rdoc doesn't like 75: # calling require with a variable number of arguments. 76: @container.__send__( :require, *parms ) 77: end
A way to access the container reference being operated on from within the context.
[ show source ]
# File lib/needle/definition-context.rb, line 46 46: def this_container 47: @container 48: end
Delegate to Container#use on the current container, but yields the definition context instead of the container.
[ show source ]
# File lib/needle/definition-context.rb, line 81 81: def use( opts, &block ) 82: use! @container.defaults.merge( opts ), &block 83: end
Delegate to Container#use! on the current container, but yields the definition context instead of the container.
[ show source ]
# File lib/needle/definition-context.rb, line 87 87: def use!( opts ) 88: original = @container.use!( opts ) 89: 90: if block_given? 91: begin 92: yield self 93: ensure 94: @container.use! original 95: end 96: end 97: 98: original 99: end