Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
README.rdoc
Rakefile

README.rdoc

yadic.rb

A lightweight dependency injection container for Ruby. Inspired by Yadic (code.google.com/p/yadic/).

Installation

gem install yadic

Usage

Some test classes:

class Foo
  def msg() 'foo' end
end     

class Bar
  def initialize(messenger)
    @messenger = messenger
  end

  def show
    @messenger.msg
  end
end

Create a container:

require 'yadic'

container = Yadic::Container.new

Add a class with a nullary constructor:

container.add(:foo, Foo)

Add a class with dependencies:

container.add(:bar) { |c| Bar.new(c[:foo]) }

Get an instance:

bar = container[:bar]
bar.show # => 'foo'

You can decorate things that have already been put in the container:

class Embelish
  include Decorator

  def msg
    "fancy #{@wrapped.msg}"
  end
end

container.decorate(:foo, Embelish)

container[:foo].msg => 'fancy foo'

(The Decorator mixin is included in yadic.)

Something went wrong with that request. Please try again.