Medicine

Gem Version Code Climate Build Status Coverage Status

Simple Dependency Injection for Ruby

Find yourself passing dependencies in to the initalizer? Medicine makes this declarative.

class CastVote
  include Medicine.di

  dependency :votes_repo, default: -> { Vote }

  def call(entry_id)
    vote_repo.create!(entry_id: entry_id)
  end
end


cast_vote = CastVote.new
cast_vote.call(3)

In this example Medicine adds a private method called vote_repo which returns Vote.

Injecting a dependency

vote_repo = double('VoteRepo')
cast_vote = CastVote.new(vote_repo: vote_repo)

If you want to arguments other than the dependencies in to the constructor don't forget to invoke super:

def initialize(arg1, arg2, dependencies = {})
  @arg1 = arg1
  @arg2 = arg2
  super(dependencies)
end

Required dependencies

dependency :vote_repo

When no default is specified and is not injected an exception will be raised on initialization.

Default dependencies

dependency :vote_repo, default: :vote
dependency :vote_repo, default: :Vote
dependency :vote_repo, default: 'Vote'
dependency :vote_repo, default: -> { Vote }

The above examples will expose a method called vote_repo which returns the Vote class as the default dependency.