New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple_injector

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple_injector

  • 0.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Rubocop Rubycritic Tests

SimpleInjector

Installation

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

Usage

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

License

The code is available as open source under the terms of the MIT License.

FAQs

Package last updated on 24 Apr 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc