Sha256: 3b19dd33869f689eab64c0ddec02a4f2c941e44b90146aea859de7028c49f3fe

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

Namespaces allow you to organize your services. The feature has many different applications, including:

# Third-parties may distribute Needle-enabled libraries without worrying about their choice of service names conflicting with the service names of their clients.
# Developers may organize complex applications into modules, and the service definitions may be stored in the registry to reflect that organization.
# Services deeper in the hierarchy may override services higher up.

Creating a namespace is as easy as invoking the @#namespace@ method of the registry (or of another namespace):

<pre>
  registry.namespace :stuff
</pre>

This would create a new namespace in the registry called @:stuff@. The application may then proceed to register services inside that namespace:

<pre>
  registry.stuff.register( :foo ) { Bar.new }
  ...
  svc = registry.stuff.foo
</pre>

Here's a tip: _namespaces are just a special kind of service._ This means that you can access namespaces in the same ways that you can access services:

<pre>
  svc = registry[:stuff][:foo]
</pre>

h3. Convenience Methods

Because it is often the case that you will be creating a namespace and then immediately registering services on it, you can pass a block to @namespace@. The block will receive a reference to the new namespace:

<pre>
  registry.namespace :stuff do |spc|
    spc.register( :foo ) { Bar.new }
    ...
  end
</pre>

And, to mirror the @namespace@ method, there is also a @namespace!@ method. This method creates a new namespace and then does a @define!@ call on that namespace.

<pre>
  registry.namespace! :stuff do
    foo { Bar.new }
    ...
  end
</pre>

The above code would create a new namespace called @:stuff@ in the registry, and would then proceed to register a service called @:foo@ in the new namespace.

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
needle-0.6.0 doc/manual/parts/02_namespaces.txt
needle-0.9.0 doc/manual/parts/02_namespaces.txt