Skip to content

benbc/yadic.rb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages