Project

hinge

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Trivial dependency resolver using introspection
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.13
~> 10.0
 Project Readme

Hinge

Hinge is a trivial dependency resolver. With Hinge, you can write down object dependencies in a linear fashion to have a simple overview of them.

class Deps
  # This method implements building a logger. It has no dependencies.
  def build_logger
    Logger.new($stdout)
  end

  # This method builds an instance of the `Processor` class you want to use,
  # initialized with the logger from the previous method.
  # This matching is done by the name of the parameter!
  def build_processor(logger)
    Processor.new(logger)
  end

  # Named parameters can be used as well!
  def build_runner(logger:)
    Runner.new(logger)
  end
end

deps = Deps.new
resolver = Hinge.resolver(deps)
processor = resolver.resolve(:processor)
resolver.resolve(:processor).equal?(processor) # => true (the same object is returned as last time)

And that's all!

Varying dependencies depending on the environment can now easily be handled in a number of ways, ranging from if/else or case constructs to inheritance or overriding methods of Deps.

Installation

Add this line to your application's Gemfile:

gem 'hinge'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hinge

Development

Tests are written in RSpec.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/schnittchen/hinge.