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 41 41: def initialize( container ) 42: @container = container 43: end
Delegates to Container#has_key?.
[ show source ]
# File lib/needle/definition-context.rb, line 103 103: def has_key?( name ) 104: @container.has_key?( name ) 105: end
Delegate to Container#intercept.
[ show source ]
# File lib/needle/definition-context.rb, line 52 52: def intercept( name ) 53: @container.intercept( name ) 54: end
Delegates to Container#knows_key?.
[ show source ]
# File lib/needle/definition-context.rb, line 108 108: def knows_key?( name ) 109: @container.knows_key?( name ) 110: 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 119 119: def method_missing( sym, *args, &block ) 120: if block.nil? 121: @container.get( sym, *args ) 122: else 123: @container.register( sym, *args, &block ) 124: end 125: end
Delegate to Container#namespace.
[ show source ]
# File lib/needle/definition-context.rb, line 57 57: def namespace( *parms, &block ) 58: @container.namespace( *parms, &block ) 59: end
Alias for namespace_define!
Delegate to Container#define on the new namespace.
[ show source ]
# File lib/needle/definition-context.rb, line 69 69: def namespace_define( *parms, &block ) 70: @container.namespace( *parms ) { |ns| ns.define( &block ) } 71: end
Delegate to Container#namespace_define!.
[ show source ]
# File lib/needle/definition-context.rb, line 62 62: def namespace_define!( *parms, &block ) 63: @container.namespace_define!( *parms, &block ) 64: end
Delegate to Container#require on the current container.
[ show source ]
# File lib/needle/definition-context.rb, line 74 74: def require( *parms ) 75: # this is necessary to work around an rdoc bug...rdoc doesn't like 76: # calling require with a variable number of arguments. 77: @container.__send__( :require, *parms ) 78: end
A way to access the container reference being operated on from within the context.
[ show source ]
# File lib/needle/definition-context.rb, line 47 47: def this_container 48: @container 49: 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 82 82: def use( opts, &block ) 83: use! @container.defaults.merge( opts ), &block 84: 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 88 88: def use!( opts ) 89: original = @container.use!( opts ) 90: 91: if block_given? 92: begin 93: yield self 94: ensure 95: @container.use! original 96: end 97: end 98: 99: original 100: end