leandrosilva / syringe

A very lightweight dependency injection container for Ruby.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

syringe

A very lightweight dependency injection container for Ruby.

It born from a real need in one of my current projects (at Locaweb). I'm only scratching our itch. If you have the same itch, join us!

Special Thanks

Hey Jim Weirich, thanks for initial code and inspiration. You are the man!

onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc

Install

gem install syringe --pre -s http://gemcutter.org

Examples

First example with taste of service locator pattern

# on application bootstrap
container = Syringe::Container.new
container.register(:service_uri) { |container| 'http://services.syringe.org/api' }
container.register(:service_consumer) { |container| ServiceConsumer.new(container[:service_uri]) }

...

# anywhere in the code
puts container[:service_uri]   # http://services.syringe.org/api
puts container.service_uri     # http://services.syringe.org/api

Second example with the best taste of dependency injection

# on application bootstrap
default_container = Syringe::Container.default
default_container.register(:service_uri) { |container| 'http://services.syringe.org/api' }

...

# in some class
class ServiceConsumer
  inject :service_uri   # it will create a new method and instance variable with that name
end

...

# anywhere in the code
service_consumer = ServiceConsumer.new
puts service_consumer.service_uri   # 'http://services.syringe.org/api'

See more on

github.com/leandrosilva/syringe/tree/master/spec

Copyright © 2010 Leandro Silva (CodeZone) <leandrodoze@gmail.com>. See LICENSE for details.

About

A very lightweight dependency injection container for Ruby.

License:MIT License


Languages

Language:Ruby 100.0%