Strudel

Gem Version CI Code Quality Coverage Inline docs

Strudel is a dependency injection container for Ruby. It's a way to organize your Ruby application to take advantage of the dependency inversion principle.

Why another DI framework?

Strudel is not a framework. It's one class that serves as a container only. No auto-injection. That means no polluting your classes with injection metaprogramming. You have full, explicit control over how your services are constructed.

Honestly, you may not even need Strudel or any other DI library. If you are passing your dependencies through your class constructors, you're already doing dependency injection! Strudel simply helps you organize your services and dependencies in one place.

But Ruby Doesn't Need Dependency Injection!

You may have read this post by David Heineimer Hansson. However he didn't address the primary benefit of DI, explicitly defining dependencies. I also happen to think that patching code at runtime for testing is an anti-pattern worth avoiding.

Installation

Add this line to your application's Gemfile:

gem 'strudel'

Getting Started

Fist create a new instance of Strudel.

require 'strudel'
app = Strudel.new

The methods you will use most often are get and set. These allow you to create and access services.

# Set a static service
app[:api_url] = 'http://example.com/api'

# Set a shared service
app.set(:api) do
  RestApi.new(app[:api_url])
end

# Now we can access the api service
app[:api].request

In this example, we set up and use an api service.

  • First we use the []= method to create a static service called api_url.
  • Then we create a service called api. This time we pass a block into the set method. This allows the service to be instantiated asynchronously. The RestApi instance won't be created until we use it on the final line.
  • On the last line, we use the [] method to retrieve the instance of RestApi and call a request method on it. Because of the way we defined the api service, the api.url parameter will be passed into the RestApi constructor when it is created.

Once it's constructed, the api service will be cached, so if we call it again, Strudel will use the same instance of RestApi.

See the API Documentation for more information or to learn about the other available methods:

  • []
  • []=
  • set
  • factory
  • protect
  • extend
  • each
  • include?

API Documentation

API documentation can be found at rubydoc.info, or you can generate HTML documentation with yard.

bin/yard

Credits

Strudel is a port of the JavaScript library papaya by Justin Howard.

Papaya is originally inspired by Pimple, a library for PHP by Fabien Potencier.

License

The gem is available as open source under the terms of the MIT License.