Class: Needle::Registry

Inherits:
Container show all
Defined in:
lib/needle/registry.rb

Overview

Registry is a specialization of Container, with additional functionality for bootstrapping basic services into a new registry. It also supports a #define! method for easily registering new services.

Usage:

require 'needle'

registry = Needle::Registry.new
registry.register( :foo ) { Foo.new }
registry.register( :bar ) { |c| Bar.new( c.foo ) }

bar = registry.bar

Instance Attribute Summary

Attributes inherited from Container

#defaults, #name, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Container

#builder, #define, #define!, #descended_from?, #find_definition, #get, #has_key?, #intercept, #keys, #knows_key?, #method_missing, #namespace, #namespace_define, #namespace_define!, #pipeline, #register, #require, #respond_to?, #root, #use, #use!

Constructor Details

#initialize(opts = {}) {|_self| ... } ⇒ Registry

Instantiate a new Registry. The options hash may include the following keys:

:logs

options used to initialize the logger factory. The value to this key should be another hash.

:parent

The parent container of this registry.

:name

The name of this registry.

If a block is given, the constructed registry instance is yielded to it.

Usage:

registry = Needle::Registry.new

or

registry = Needle::Registry.new do |reg|
  reg.register( :add ) { Adder.new }
end

or

registry = Needle::Registry.new(
  :logs => { :filename => "/dev/null" }
)

Yields:

  • (_self)

Yield Parameters:



104
105
106
107
108
# File 'lib/needle/registry.rb', line 104

def initialize( opts={} )
  super( opts[:parent], opts[:name] )
  bootstrap( opts ) if parent.nil?
  yield( self ) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Needle::Container

Class Method Details

.define(*parms, &block) ⇒ Object

Instantiate a new Registry (via #new) and immediately invoke #define using the given block.

Usage:

registry = Needle::Registry.define do |b|
  b.add { Adder.new }
  ...
end

adder = registry.add

Raises:



74
75
76
77
# File 'lib/needle/registry.rb', line 74

def self.define( *parms, &block )
  raise NeedleError, "needs a block" if block.nil?
  new( *parms ) { |reg| reg.define( &block ) }
end

.define!(*parms, &block) ⇒ Object

Instantiate a new Registry (via #new) and immediately invoke #define! using the given block.

Usage:

registry = Needle::Registry.define! do
  add { Adder.new }
  ...
end

adder = registry.add

Raises:



58
59
60
61
# File 'lib/needle/registry.rb', line 58

def self.define!( *parms, &block )
  raise NeedleError, "needs a block" if block.nil?
  new( *parms ) { |reg| reg.define!( &block ) }
end

Instance Method Details

#fullnameObject

Returns nil, unless the registry has a parent, in which case it acts like Container#fullname. Registries are usually unnamed containers.



112
113
114
# File 'lib/needle/registry.rb', line 112

def fullname
  parent ? super : nil
end