Project

ringu

0.0
No commit activity in last 3 years
No release in over 3 years
A simple dependency injection framework for ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Ringu

IMPORTANT: Not ready for production

Installation

Add this line to your application's Gemfile:

gem 'ringu', '~> 0.1.1.pre.alpha'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ringu

Usage

Create container

Create file container.rb in app/deps directory

mkdir app/deps

echo 'module Container\n  include Ringu::Container\n\n  # Deps\n  # register :name, class\nend\n' >> app/deps/container.rb

Example example/app/deps/container.rb

Register your dependencies

module Container
  include Ringu::Container

  register :characters_fetcher, CharactersFetcher  
  register :http_client, Faraday
end

Inject your dependencies

class CharactersFetcher
  include Container::Inject
  
  URL = "https://rickandmortyapi.com/api/character"

  inject :http_client
  inject :characters_parser, CharactersParser

  def fetch
    response = http_client.get(URL)
    characters_parser.parse(response.body)
  end  
end

Example example/app/fetchers/characters_fetcher.rb

Example test example/test/fetchers/characters_fetcher_test.rb

Resolve dependency

Container.resolve(:characters_fetcher)

or

class CharactersController < ApplicationController
  include Container::Resolve
  
  resolve :characters_fetcher

  # GET /characters
  def index
    @characters = characters_fetcher.fetch

    render json: @characters
  end
end

Example example/app/fetchers/characters_fetcher.rb

Example test example/test/fetchers/characters_fetcher_test.rb

Example rails project

Rails project

TODO

  • Add tests
  • Add docs and more exmaples
  • Refactor

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ramon-sg/ringu.

License

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