Method: Needle::Container#namespace_define

Defined in:
lib/needle/container.rb

#namespace_define(name, opts = {}, &block) ⇒ Object

Create a new namespace within the container, with the given name. The block (which is required) will be passed to Container#define on the new namespace.

For the curious, namespaces are simply services that are implemented by Container. The two statements are really identical:

container.namespace( :calc )
container.register( :calc ) { |c,p| Needle::Container.new( c, p.name ) }

Note that this means that namespaces may be singletons or prototypes, or have immediate or deferred instantiation, and so forth. (The default of immediate, singleton instantiation is sufficient for 99% of the things you’ll use namespaces for.)

Usage:

container.namespace_define( :operations ) do |b|
  b.add { Adder.new }
  ...
end

adder = container.calc.operations.add

Note: this method will immediately instantiate the new namespace, unlike #namespace. If you want instantiation of the namespace to be deferred, either use a deferring service model (like :singleton_deferred) or create the namespace via #namespace.

Raises:

  • (ArgumentError)


248
249
250
251
252
# File 'lib/needle/container.rb', line 248

def namespace_define( name, opts={}, &block )
  raise ArgumentError, "block expected" unless block
  namespace( name, opts ) { |ns| ns.define( &block ) }
  self[name]
end