项目作者: schnittchen

项目描述 :
Trivial dependency resolver for Ruby, using introspection
高级语言: Ruby
项目地址: git://github.com/schnittchen/hinge.git
创建时间: 2017-03-08T12:10:08Z
项目社区:https://github.com/schnittchen/hinge

开源协议:

下载


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.

  1. class Deps
  2. # This method implements building a logger. It has no dependencies.
  3. def build_logger
  4. Logger.new($stdout)
  5. end
  6. # This method builds an instance of the `Processor` class you want to use,
  7. # initialized with the logger from the previous method.
  8. # This matching is done by the name of the parameter!
  9. def build_processor(logger)
  10. Processor.new(logger)
  11. end
  12. # Named parameters can be used as well!
  13. def build_runner(logger:)
  14. Runner.new(logger)
  15. end
  16. end
  17. deps = Deps.new
  18. resolver = Hinge.resolver(deps)
  19. processor = resolver.resolve(:processor)
  20. 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:

  1. gem 'hinge'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ 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.