Sha256: 493e1f86f85e8aefb456c9dd147c2e9766279ea89c345a6a3f58676585477475

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

AttrInject is very straightforward to use. Just require the AttrInject library in every service implementation and use the new @attr_inject@ macro to specify which other services the class depends on:

<pre>
  require 'needle/extras/attr-inject'

  class Foo
    attr_inject :bar
    attr_inject :baz, :blah

    def frobnicate
      @bar + @baz / @blah
    end
  end
</pre>

The @attr_inject@ macro does not create any accessors--it only declares the dependencies that the corresponding service has. Then, when you register the service, you specify one of the @inject@ service models:

<pre>
  require 'needle'
  require 'needle/extras'
  ...
  reg.require_library 'needle/extras'
  reg.define do |b|
    b.bar { 5 }
    b.baz { 10 }
    b.blah { Math::PI }

    b.foo( :model => :singleton_inject ) { Foo.new }
  end
</pre>

The @singleton_inject@ service model is just like @singleton@, but it will also automatically inject all of the declared dependencies into the new service. Thus, invoking @#frobnicate@ on the @foo@ service would compute and return (in this case) @5 + 10 / PI@.

This approach has the benefit of reducing the amount of initialization code you have to write. On the other hand, it more tightly couples your implementation code to Needle itself.

Version data entries

1 entries across 1 versions & 1 rubygems

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