
Security News
Researcher Exposes Zero-Day Clickjacking Vulnerabilities in Major Password Managers
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Install the gem and add to the application's Gemfile by executing:
$ bundle add simple_injector
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install simple_injector
First, declare your contract to your class
class CreateUserContract < SimpleInjector::Contract
register :api_client, -> { ApiClient.new }
end
The register
method receive a key, as a identifier of that instance and a proc that returns the object to be injectable into class
class CreateUser
include SimpleInjector
contract CreateUserContract
attr_injector :api_client
def initialize(user_params)
@user_params = user_params
end
def create
api_client.post @user_params
end
end
In your service class, include the SimpleInjector
module. This will be add the contract
and attr_injector
methods
The contract
method receives a class, your contract defined previously
The attr_injector
receives the key, has to be the same key defined in the contract and add a method in service class to retrieve the instance.
If you define more than one instance on contract class, you could add an attr_injector
for each instance. Ex:
class CreateUserContract < SimpleInjector::Contract
register :api_client, -> { ApiClient.new }
register :notify_user, -> { NotifyUser.new }
register :user_model, -> { User }
end
class CreateUser
include SimpleInjector
contract CreateUserContract
attr_injector :api_client
attr_injector :notify_user
attr_injector :user_model
def initialize(user_params)
@user_params = user_params
end
def create
user = user_model.save @user_params
api_client.post '/users/create/token', { id: user.id }
notify_user.notify(user)
end
end
The code is available as open source under the terms of the MIT License.
FAQs
Unknown package
We found that simple_injector demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Hacker Demonstrates How Easy It Is To Steal Data From Popular Password Managers
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.