a lightweight dependency injection framework for Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib Version 0.1.1 Sep 27, 2012
spec allow block parameters Sep 27, 2012
.gitignore The simplest thing that works Feb 20, 2012
Gemfile Added guard. Apr 9, 2012
Guardfile Added guard. Apr 9, 2012
LICENSE Create LICENSE May 31, 2016
README.md add disclaimer Mar 9, 2012
Rakefile spec tasks for rakefile. Apr 9, 2012
morphine.gemspec Fix gemspec. Mar 21, 2012



NOTE: I am in the process of extracting dependency injection features from Gaug.es. Currently, the only feature of this library is memoization. I am not dumb or inexperienced. I'm just not done yet.

Morphine is a lightweight dependency injection framework for Ruby. It uses a simple Ruby DSL to ease the pain of wiring your dependencies together.


Create a container for your dependencies and include the Morphine module.

class Application
  include Morphine

  register :track_service do
    KestrelTrackService.new(kestrel_client, config.tracking_queue)

  register :track_processor do
    KestrelTrackProcessor.new(blocking_kestrel_client, config.tracking_queue)


  register :kestrel_client do
    c = config['kestrel'].dup
    Kestrel::Client.new(c.delete('servers'), c.symbolize_keys)

  register :blocking_kestrel_client do

Create an instance of your container, and use that to load your dependencies

$app = Application.new

get '/track.gif' do

But I don't need dependency injection in Ruby!

Many argue that you don't need dependency injection in dynamic languages like Ruby. What they are really saying is you don't need a complicated dependency injection framework, and they're right.

That's why Morphine is an extremely simple library.


If you find what looks like a bug:

  1. Check the GitHub issue tracker to see if anyone else has reported issue.
  2. If you don't see anything, create an issue with information on how to reproduce it.

If you want to contribute an enhancement or a fix:

  1. Fork the project on GitHub.
  2. Make your changes with tests.
  3. Commit the changes without making changes to the Rakefile, Gemfile, gemspec, or any other files that aren't related to your enhancement or fix
  4. Send a pull request.