Sha256: 666718d601e7ec0cc6117f6e5e0d1dd5d6b659d03c0e133f1ff1e16cdc339c7e

Contents?: true

Size: 1.77 KB

Versions: 1

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 @register!@ method, there is also a @namespace!@ method. This method creates a new namespace and then does a @register!@ 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

1 entries across 1 versions & 1 rubygems

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