Repository is archived
No commit activity in last 3 years
No release in over 3 years
A Sinatra wrapper for a fully customizable Dependency injection system
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Dependency Injection for Sinatra

Sinatra::DependencyInjection adds a helper method, called container, to access to your ruby dependency injection container

Installation

Just add the gem to your Gemfile

gem 'dependency_injection-sinatra'

Or simply install it using rubygems:

gem install dependency_injection-sinatra

Usage

Classic Application

In a classic application simply require the helper, and start using it:

require 'sinatra'
require 'sinatra/dependency_injection'

# define a route that uses the helper
get '/' do
  json container.get('awesome_service').find_trending
end

# The rest of your classic application code goes here...

Modular Application

In a modular application you need to require the helper, and then tell the application you will use it:

require 'sinatra/base'
require 'sinatra/dependency_injection'

class MyApp < Sinatra::Base
  register Sinatra::DependencyInjection

  # define a route that uses the helper
  get '/' do
    json container.get('awesome_service').find_trending
  end

  # The rest of your modular application code goes here...
end

Configuration path

By default it will try to load services.yml configuration file, but if your configuration file is stored in another place, you can easily change it:

require 'sinatra/base'
require 'sinatra/dependency_injection'

class MyApp < Sinatra::Base
  register Sinatra::DependencyInjection

  dependency_injection_path 'config/services.yml'

  # The rest of your module application code goes here...
end