Sha256: d6c82cb8049b53f9622f474e92ebf830c5a0fc76ff1bbaf1effc142b7d059994

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

For developers of service libraries, RequireLibrary provides a hook for registering their libraries with Needle:

<pre>
  module Foo
    module Bar

      def register_services( container )
        ...
      end
      module_function :register_services

      if defined?(Needle.register_library)
        Needle.register_library( 'foo/bar', self )
      end

    end
  end
</pre>

The @#register_services@ method is Needle's standard callback for registering a library's services with the given container.

The next lines, though, check to see if @Needle.register_library@ is defined. This allows the library to be used even when Needle-Extras is not loaded, or even installed. If the method exists, it is invoked with the @require@ path of the file, and the module reference that contains the @#register_services@ method.

Then, consumers of the library can load it using RequireLibrary as follows:

<pre>
  require 'needle'

  reg = Needle::Registry.new
  reg.require_library 'foo/bar'
  ...
</pre>

The call to @Container#require_library@ invokes @Kernel#require@, and then looks to see if there is a hook registered for the @'foo/bar'@ path. If there is, the hook is invoked, which (by default) invokes the @#register_services@ method, passing the current container as the parameter.

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
needle-extras-1.0.0 doc/manual/parts/requirelibrary_usage.txt