https://github.com/krisleech/medicine
Simple dependency injection for Ruby objects
https://github.com/krisleech/medicine
Last synced: about 1 year ago
JSON representation
Simple dependency injection for Ruby objects
- Host: GitHub
- URL: https://github.com/krisleech/medicine
- Owner: krisleech
- License: mit
- Created: 2014-11-04T21:17:53.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2016-11-01T17:11:19.000Z (over 9 years ago)
- Last Synced: 2025-04-13T03:06:52.962Z (about 1 year ago)
- Language: Ruby
- Size: 40 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Medicine
[](http://badge.fury.io/rb/medicine)
[](https://codeclimate.com/github/krisleech/medicine)
[](https://travis-ci.org/krisleech/medicine)
[](https://coveralls.io/r/krisleech/medicine?branch=master)
[](http://inch-ci.org/github/krisleech/medicine)
Simple Dependency Injection for Ruby
Find yourself injecting dependencies via the initalizer or a setter method?
Medicine makes this declarative.
## Usage
Include the Medicine module and declare the dependencies with `dependency`.
```ruby
class CastVote
include Medicine.di
dependency :votes_repo, default: -> { Vote }
def call
votes_repo # => Vote
end
end
```
For each dependency declared a private method is defined which returns the
dependency.
### Without injection
```ruby
command = CastVote.new
```
In the above case the `votes_repo` method will return `Vote`.
If no dependency is injected the default will be used.
Specifying a default is optional and if a dependency is not injected and
there is no default an error will be raised if the dependencies method is
invoked.
### Injecting via initializer
```ruby
command = CastVote.new(votes_repo: double)
```
In the above case `votes_repo` will return the double.
If you try and inject a dependency which has not been declared an error is
raised.
### Injecting via a setter
```ruby
command = CastVote.new
command.inject_depdendency(:vote_repo, double)
```
In the above case `votes_repo` will return the double.
If you try and inject a dependency which has not been declared an error is
raised.
### Required dependencies
```ruby
dependency :vote_repo
```
When no default is specified the dependency must be injected via the
constructor or setter an otherwise an exception will be raised.
### Default dependencies
```ruby
dependency :vote_repo, default: Vote
dependency :vote_repo, default: :vote
dependency :vote_repo, default: :Vote
dependency :vote_repo, default: 'Vote'
dependency :vote_repo, default: -> { Vote }
```
All the above examples will expose a method called `vote_repo` which returns the
`Vote` class as the default dependency.
### Already got an initializer?
If you want to pass arguments other than the dependencies in to the constructor
don't forget to invoke `super`:
```ruby
def initialize(arg1, arg2, dependencies = {})
@arg1 = arg1
@arg2 = arg2
super(dependencies)
end
```
## Compatibility
Tested with MRI 2.1+ and Rubinius.
See the [build status](https://travis-ci.org/krisleech/medicine) for details.
## Running Specs
```
rspec spec
```