Class: Needle::DefinitionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/needle/definition-context.rb

Overview

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.)

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.



40
41
42
# File 'lib/needle/definition-context.rb', line 40

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.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/needle/definition-context.rb', line 86

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.



51
52
53
# File 'lib/needle/definition-context.rb', line 51

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

#namespace(*parms, &block) ⇒ Object

Delegate to Container#namespace.



56
57
58
# File 'lib/needle/definition-context.rb', line 56

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

#namespace_define(*parms, &block) ⇒ Object

Delegate to Container#define on the new namespace.



68
69
70
# File 'lib/needle/definition-context.rb', line 68

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!.



61
62
63
# File 'lib/needle/definition-context.rb', line 61

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

#require(*parms) ⇒ Object

Delegate to Container#require on the current container.



73
74
75
76
77
# File 'lib/needle/definition-context.rb', line 73

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.



46
47
48
# File 'lib/needle/definition-context.rb', line 46

def this_container
  @container
end