head
operator(-) keyreg = Needle::Registry.newThis will create a new Registry instance. You can also send a block to #new, in which case the new registry will be yielded to it:
reg = Needle::Registry.new do |r| ... endThere are two other factory methods you can use for creating a Registry instance. Both require a block.
r1 = Needle::Registry.define do |builder| ... end r2 = Needle::Registry.define! do ... endRegistry#define creates a "builder" object that you can use define services more conveniently. Register#define! (with a bang\) does the same thing, but evaluates the block within the context of the builder.)> operator(-) key
reg.register( :foo \) { Foo.new }The (first\) parameter to #register is the name of the service, and the block should return the implementation of the service. If needed, the block can accept two parameters--the container that the service is being registered with, and an object that represents the service being defined (called a "service point"\):
reg.register( :foo \) do |container,point| Foo.new( container[:bar], point.fullname \) endYou can also use Container#define and Container#define! to register services. These approaches are friendlier if you are needing to register several services at once.
reg.define do |builder| builder.foo { Foo.new } builder.bar { |c,p| Bar.new( c[:foo], p.name \) } end reg.define! do baz { |c,p| Baz.new( c[:bar], p.name \) } zoom { Buggy.new } endContainer#define yields a new "builder" object to the block. Messages sent to the builder are interpreted as service names, and if a block is sent with the message, a new service is registered under that name. Container#define! does likewise, except it evaluates the block within the context of the builder object. If you do not pass a block to #define, it will return the builder object, so you could do something like the following if you only need to define one or two services:
reg.define.foo { ... }Lastly, you can get the builder directly and add services using it:
builder = reg.builder builder.baz { ... } builder.bar { ... }(This last is the same as calling #define without arguments, but is more readable if you intend to use the builder object multiple times.\) )> operator(-) key
svc = registry[:foo] svc.do_something_interestingA more convenient (but slightly more peril-fraught\) approach is to send the name of the method to the registry as a message:
svc = registry.fooBe aware that this latter approach will only work when the service name does not conflict with the name of an existing method on the container. For example, if you were to do:
registry.register( :hash \) { "hello, world" } p registry.hashYou would get the hash value of the registry object, instead of the value value of the service (which would be "hello, world"\).)> operator(-) key
registry.register( :foo, :model => :prototype \) {...} registry.define.bar( :model => :threaded \) {...} registry.define! do baz( :model => :singleton_deferred \) {...} ... end ...)> operator(-) key
registry.namespace( :stuff \)This will create a namespace in the registry, called stuff. If you send a block as well, the block will be invoked (with the new namespace yielded to it\) the first time the namespace is requested:
registry.namespace( :stuff \) do |ns| ns.register( :foo \) {...} ns.define.bar {...} ns.define! do baz {...} buf {...} end endBecause it is so common to immediately define services on the new namespace, there are some convenience methods to make this more... convenient.
registry.namespace_define!( :stuff \) do foo {...} bar {...} baz {...} end registry.namespace_define( :more_stuff \) do |b| b.blah {...} b.argh {...} b.hack {...} endThe first one, above, creates the namespace and calls Container#define!. The second creates the namespace and calls Container#define. In both cases, _the namespace is created immediately_, unlike Container#namespace which only creates the namespace when it is first requested. Lastly, note that namespace's are just special services. Thus, you can pass options to the namespace methods just as you can with Container#register and friends.)> operator(-) key
logger = registry.logs.get( "a name for my logger" \) logger.debug "This is a debug message" logger.info "This is an informational message" ... logger2 = registry.log_for( "another logger name" \) ...The two approaches shown above are identical--the second approach (using the @log_for@ service\) is just a convenience for @logs.get@. Log messages are written, by default, to a file called "needle.log", in the same directory that the application was invoked from. You can also use a logging interceptor to automatically log all external method invocations on a service. This includes method entry and exit, as well as any exceptions that are raised inside the method.
registry.register( :foo \) { ... } registry.intercept( :foo \).with { |r| r.logging_interceptor } foo.something foo.another_method( 1, 2, 3 \)See the chapter in the "User's Manual":http://needle.rubyforge.org about logging for more information on how to use and configure loggers.)> operator(-) key
registry.register( :foo \) { ... } registry.intercept( :foo \). with { |r| r.logging_interceptor }. with_options :exclude => [ 'foo', 'bar(>4\)', '*(<2\)' ]The above will exclude from interception any method named 'foo', or any invocation of 'bar' with more than 4 arguments, or any method invocation with fewer than two arguments. You can also give an array of patterns to _include_. These cause methods to be explicitly intercepted even if they match an exclude pattern:
registry.register( :foo \) { ... } registry.intercept( :foo \). with { |r| r.logging_interceptor }. with_options :exclude => [ 'foo', 'bar(>4\)', '*(<2\)' ], :include => [ 'baz' ] foo = registry.foo foo.bazThis would result in the call to #baz being intercepted, even though it matches an exclude pattern (@*(<2\)@\).)> operator(-) key
module A module B def register_services( container \) ... end module_function :register_services end endIf the library has been implemented in this way, you can simply do a require of the library and then invoke the @register_services@ method. There is a convenience method in Container for doing this. Just call Container#require, passing the file to require and a string (or symbol\) identifying the name of the module that contains the registration method. You can also pass a symbol as the third parameter naming the registration method, but it defaults to @:register_services@.
require 'a/b' A::B.register_services( container \) # or container.require( 'a/b', "A::B" \)The definition context (i.e., the "builder" object\) also supports the require method, so you can do:
container.define do |b| b.require "a/b", "A::B" b.foo { ... } ... end)> operator(-) key