A simple macro, akin to def_delegator for plain and simple dependency injection.

I'm of the opinion that if you are going to have a convention for dependency injection, then why not make a macro method. Then, it's easy to components involved in the dependency injection straightforward.

This is plain simple dependency injection and frankly, if you don't like it at least you have a macro to search on instead of scattered :attr_writer declarations with their corresponding private :attr_reader methods.


Add this line to your application's Gemfile:

gem 'dependency_injector'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dependency_injector


class Blog
  extend DependencyInjector
  def_injector(:request_locator) { Location.public_method(:locate) }

=> true
=> NoMethodError: private method `request_locator' called for #<Blog:0x00000103408c20>
$ = -> { }
=> #<Proc:[email protected](irb):3 (lambda)>


  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request