Project

yadic

0.0
No commit activity in last 3 years
No release in over 3 years
A lightweight dependency injection container.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

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.)