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.



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

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.



119
120
121
122
123
124
125
# File 'lib/needle/definition-context.rb', line 119

def method_missing( sym, *args, &block )
  if block.nil?
    @container.get( sym, *args )
  else
    @container.register( sym, *args, &block )
  end
end

Instance Method Details

#has_key?(name) ⇒ Boolean

Delegates to Container#has_key?.

Returns:

  • (Boolean)


103
104
105
# File 'lib/needle/definition-context.rb', line 103

def has_key?( name )
  @container.has_key?( name )
end

#intercept(name) ⇒ Object

Delegate to Container#intercept.



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

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

#knows_key?(name) ⇒ Boolean

Delegates to Container#knows_key?.

Returns:

  • (Boolean)


108
109
110
# File 'lib/needle/definition-context.rb', line 108

def knows_key?( name )
  @container.knows_key?( name )
end

#namespace(*parms, &block) ⇒ Object

Delegate to Container#namespace.



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

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

#namespace_define(*parms, &block) ⇒ Object

Delegate to Container#define on the new namespace.



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

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



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

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

#require(*parms) ⇒ Object

Delegate to Container#require on the current container.



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

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.



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

def this_container
  @container
end

#use(opts, &block) ⇒ Object

Delegate to Container#use on the current container, but yields the definition context instead of the container.



82
83
84
# File 'lib/needle/definition-context.rb', line 82

def use( opts, &block )
  use! @container.defaults.merge( opts ), &block
end

#use!(opts) ⇒ Object

Delegate to Container#use! on the current container, but yields the definition context instead of the container.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/needle/definition-context.rb', line 88

def use!( opts )
  original = @container.use!( opts )

  if block_given?
    begin
      yield self
    ensure
      @container.use! original
    end
  end

  original
end