Skip to content
A tiny dependency injection container for Ruby
Ruby Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin
lib
spec
.gitignore
.rspec
.rubocop.yml
.travis.yml
.yardopts
Gemfile
LICENSE.txt
README.md
Rakefile
strudel.gemspec

README.md

Strudel

Gem Version Build Status Code Climate Test 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 garbage 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 egregious anti-pattern. In case you need more convincing, check out this great post by Piotr Solnica.

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.

You can’t perform that action at this time.