{"id":20904673,"url":"https://github.com/sage/sinject","last_synced_at":"2025-05-13T05:30:44.315Z","repository":{"id":51558870,"uuid":"51142276","full_name":"Sage/sinject","owner":"Sage","description":"A simple dependency injection framework for ruby.","archived":false,"fork":false,"pushed_at":"2024-04-12T09:38:55.000Z","size":101,"stargazers_count":10,"open_issues_count":8,"forks_count":2,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-18T00:06:58.162Z","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/Sage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-05T11:14:45.000Z","updated_at":"2025-04-11T11:12:11.000Z","dependencies_parsed_at":"2024-04-12T10:46:39.027Z","dependency_job_id":null,"html_url":"https://github.com/Sage/sinject","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fsinject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fsinject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fsinject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fsinject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sage","download_url":"https://codeload.github.com/Sage/sinject/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253882696,"owners_count":21978537,"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-11-18T13:18:27.479Z","updated_at":"2025-05-13T05:30:43.999Z","avatar_url":"https://github.com/Sage.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sinject [![Build Status](https://travis-ci.org/Sage/sinject.svg?branch=master)](https://travis-ci.org/Sage/sinject) [![Maintainability](https://api.codeclimate.com/v1/badges/e54f26ad98595149d072/maintainability)](https://codeclimate.com/github/Sage/sinject/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/e54f26ad98595149d072/test_coverage)](https://codeclimate.com/github/Sage/sinject/test_coverage) [![Gem Version](https://badge.fury.io/rb/sinject.svg)](https://badge.fury.io/rb/sinject)\n\nWelcome to Sinject! a simple dependency injection framework for ruby.\n\n[Note: The update to v1.0.0 contains breaking changes. All code has now been moved into the module `Sinject::*` and the register method of the container has been modified to take an options hash as opposed to requiring multiple method arguments.]\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'sinject'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install sinject\n\n## Usage\n\n**Registering dependencies**\n\nDependency objects need to be registered with the container before use, to do so you need to configure the Sinject::Container. [Note: This should be done after all code files have been required as the last step before your application starts.]\n\n    #initialize the container\n    container = Sinject::Container.new\n\n    #register your dependencies\n    container.register({ :key =\u003e :cache_store, :class =\u003e RedisCacheStore, :singleton =\u003e true })\n    container.register({ :key =\u003e :country_repository, :class =\u003e MySqlCountryRepository, :singleton =\u003e false })\n\nDependencies can be registered with the container in 2 modes:\n\n- **Single instance:**  \tThis mode ensures that only 1 instance is created for the registered dependency and that all requests to the container for that dependency return the same instance.\n- **Multi instance:**\tThis mode ensures that a new instance of the registered dependency is returned for each request received by the container.\n\nThe registration mode can be set by specifying **true** or **false** to the *`:singleton`* argument of the containers register method.\n\nDependencies that require custom initialization can be registered with an initialization block to handle the creation of the dependency, this allows you more control over how the dependency is created if required:\n\n    container.register({ :key =\u003e :cache_store, :class =\u003e RedisCacheStore, :singleton =\u003e true }) do\n        instance = RedisCacheStore.new\n        instance.host = 'http://localhost'\n        instance.port = '6369'\n        instance\n    end\n\nDependencies with a custom initialization block must return an object of the registered dependency class type, if an unexpected instance is returned then Sinject will raise a `Sinject::DependencyInitializeException`.\n\n**Assigning dependencies**\n\nTo assign a dependency to an object you need to add the dependency attribute to the class and specify the symbol key that was used to register the dependency with the SinjectContainer:\n\n    class MySqlCountryRepository\n\n\t    dependency :cache_store\n\n\t\t.....\n\tend\n\n    class CountryController \u003c ActionController::Base\n\n\t\tdependency :country_repository\n\n\t\t.....\n    end\n\nSinject will then inject the registered dependency to that object and it will be accessible via the dependency key:\n\n    country_controller.country_repository.cache_store\n\n\n**Dependency Contracts**\n\nDependency contracts can be defined to validate registered dependencies are valid for the task they are being registered for. *(If you are familiar with other type based languages then you can think of this as a kind of Interface)*\n\nTo create a dependency contract you need to create a new class with empty methods for each of the methods that the dependency needs to respond to in order to fulfill it's role:\n\n    class LoggerContract\n        def write(message)\n        end\n    end\n\nThen when registering a dependency for the role the contract is written for, you can assign the contract:\n\n    #register the dependency\n    container.register({ :key =\u003e :logger, :class =\u003e FileLogger, :singleton =\u003e false, :contract =\u003e LoggerContract })\n\nSinject will then validate that the registered dependency meets the requirements specified within the contract. If a dependency does not meet the contract requirements then 1 of the following exceptions will be raised:\n\n- `Sinject::DependencyContractMissingMethodsException` is raised when 1 or more methods from the contract could not be found on the dependency.\n- `Sinject::DependencyContractInvalidParametersException` is raised when the parameters of a contract method do not match the parameters found on a dependency method.\n\n**Dependency Groups**\n\nDependency registration groups can be created to allow groups of dependencies to be set without the need for manual registration *(e.g. to include with a gem for auto registration)*, or to allow different dependency groups to be loaded in different circumstances *(e.g. per environment)*.\n\nTo create a dependency group, create a class that inherits from the `Sinject::DependencyGroup` base class and implement the `register` \u0026 `is_valid?` methods.\n\nFor example:\n\n    #create a development only dependency group\n    class DevelopmentDependencies \u003c Sinject::DependencyGroup\n        def register(container)\n            container.register(:cache_store, LocalCacheStore, true)\n            container.register(:logger, TerminalLogger, true)\n        end\n\n        def valid?\n            Rails.env.development?\n        end\n    end\n\nTo load valid dependency groups the following method needs to be called from the container:\n\n    container.load_groups\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/sage/sinject. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n\n## License\n\nThis gem is available as open source under the terms of the\n[MIT licence](LICENSE).\n\nCopyright (c) 2018 Sage Group Plc. All rights reserved.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fsinject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsage%2Fsinject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fsinject/lists"}