Class: Needle::Container::DefinitionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/needle/container.rb

Overview

This class is used by the #define! and #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.)

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ DefinitionContext

Create a new DefinitionContext that wraps the given container. All operations performed on this context will be delegated to the container.



51
52
53
# File 'lib/needle/container.rb', line 51

def initialize( container )
  @container = container
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

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.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/needle/container.rb', line 97

def method_missing( sym, *args, &block )
  if block.nil?
    if args.empty?
      @container[sym]
    else
      super
    end
  else
    @container.register( sym, *args, &block )
  end
end

Instance Method Details

#intercept(name) ⇒ Object

Delegate to Container#intercept.



62
63
64
# File 'lib/needle/container.rb', line 62

def intercept( name )
  @container.intercept( name )
end

#namespace(*parms, &block) ⇒ Object

Delegate to Container#namespace.



67
68
69
# File 'lib/needle/container.rb', line 67

def namespace( *parms, &block )
  @container.namespace( *parms, &block )
end

#namespace_define(*parms, &block) ⇒ Object

Delegate to Container#define on the new namespace.



79
80
81
# File 'lib/needle/container.rb', line 79

def namespace_define( *parms, &block )
  @container.namespace( *parms ) { |ns| ns.define( &block ) }
end

#namespace_define!(*parms, &block) ⇒ Object Also known as: namespace!

Delegate to Container#namespace_define!.



72
73
74
# File 'lib/needle/container.rb', line 72

def namespace_define!( *parms, &block )
  @container.namespace_define!( *parms, &block )
end

#require(*parms) ⇒ Object

Delegate to Container#require on the current container.



84
85
86
87
88
# File 'lib/needle/container.rb', line 84

def require( *parms )
  # this is necessary to work around an rdoc bug...rdoc doesn't like
  # calling require with a variable number of arguments.
  @container.__send__( :require, *parms )
end

#this_containerObject

A way to access the container reference being operated on from within the context.



57
58
59
# File 'lib/needle/container.rb', line 57

def this_container
  @container
end