Wynn Netherland changelog.com/posts

Morphine - Lightweight dependency injection for Ruby

GitHubber Brandon Keepers:

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.

So Brandon created Morphine, a simple DI library for Ruby. With Morphine, you can create a DI container by including the Morphine module and specifying your dependencies.

class Application
  include Morphine

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

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

private

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

  register :blocking_kestrel_client do
    Kestrel::Client::Blocking.new(kestrel_client)
  end
end

If you feel Ruby benefits from dependency injection and want to contribute, check out the just released source on GitHub.


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00