{"id":15405581,"url":"https://github.com/krisleech/medicine","last_synced_at":"2025-04-17T01:54:00.222Z","repository":{"id":56883192,"uuid":"26189169","full_name":"krisleech/medicine","owner":"krisleech","description":"Simple dependency injection for Ruby objects","archived":false,"fork":false,"pushed_at":"2016-11-01T17:11:19.000Z","size":41,"stargazers_count":7,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T03:06:52.962Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krisleech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-04T21:17:53.000Z","updated_at":"2017-10-24T15:38:32.000Z","dependencies_parsed_at":"2022-08-20T13:10:46.540Z","dependency_job_id":null,"html_url":"https://github.com/krisleech/medicine","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fmedicine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fmedicine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fmedicine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krisleech%2Fmedicine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krisleech","download_url":"https://codeload.github.com/krisleech/medicine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249299839,"owners_count":21246915,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T16:17:15.530Z","updated_at":"2025-04-17T01:54:00.202Z","avatar_url":"https://github.com/krisleech.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Medicine\n\n[![Gem Version](https://badge.fury.io/rb/medicine.png)](http://badge.fury.io/rb/medicine)\n[![Code Climate](https://codeclimate.com/github/krisleech/medicine.png)](https://codeclimate.com/github/krisleech/medicine)\n[![Build Status](https://travis-ci.org/krisleech/medicine.png?branch=master)](https://travis-ci.org/krisleech/medicine)\n[![Coverage Status](https://coveralls.io/repos/krisleech/medicine/badge.png?branch=master)](https://coveralls.io/r/krisleech/medicine?branch=master)\n[![Inch Pages](http://inch-ci.org/github/krisleech/medicine.png)](http://inch-ci.org/github/krisleech/medicine)\n\nSimple Dependency Injection for Ruby\n\nFind yourself injecting dependencies via the initalizer or a setter method?\n\nMedicine makes this declarative.\n\n## Usage\n\nInclude the Medicine module and declare the dependencies with `dependency`.\n\n```ruby\nclass CastVote\n  include Medicine.di\n\n  dependency :votes_repo, default: -\u003e { Vote }\n\n  def call\n    votes_repo # =\u003e Vote\n  end\nend\n```\n\nFor each dependency declared a private method is defined which returns the\ndependency.\n\n### Without injection\n\n```ruby\ncommand = CastVote.new\n```\n\nIn the above case the `votes_repo` method will return `Vote`.\n\nIf no dependency is injected the default will be used.\n\nSpecifying a default is optional and if a dependency is not injected and\nthere is no default an error will be raised if the dependencies method is\ninvoked.\n\n### Injecting via initializer\n\n\n```ruby\ncommand = CastVote.new(votes_repo: double)\n```\n\nIn the above case `votes_repo` will return the double.\n\nIf you try and inject a dependency which has not been declared an error is\nraised.\n\n### Injecting via a setter\n\n```ruby\ncommand = CastVote.new\ncommand.inject_depdendency(:vote_repo, double)\n```\n\nIn the above case `votes_repo` will return the double.\n\nIf you try and inject a dependency which has not been declared an error is\nraised.\n\n### Required dependencies\n\n```ruby\ndependency :vote_repo\n```\n\nWhen no default is specified the dependency must be injected via the\nconstructor or setter an otherwise an exception will be raised.\n\n### Default dependencies\n\n```ruby\ndependency :vote_repo, default: Vote\ndependency :vote_repo, default: :vote\ndependency :vote_repo, default: :Vote\ndependency :vote_repo, default: 'Vote'\ndependency :vote_repo, default: -\u003e { Vote }\n```\n\nAll the above examples will expose a method called `vote_repo` which returns the\n`Vote` class as the default dependency.\n\n### Already got an initializer?\n\nIf you want to pass arguments other than the dependencies in to the constructor\ndon't forget to invoke `super`:\n\n```ruby\ndef initialize(arg1, arg2, dependencies = {})\n  @arg1 = arg1\n  @arg2 = arg2\n  super(dependencies)\nend\n```\n\n## Compatibility\n\nTested with MRI 2.1+ and Rubinius.\n\nSee the [build status](https://travis-ci.org/krisleech/medicine) for details.\n\n## Running Specs\n\n```\nrspec spec\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisleech%2Fmedicine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrisleech%2Fmedicine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrisleech%2Fmedicine/lists"}