Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ioc

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ioc

  • 6.4.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

IOC

Minimal dependency injection container for ruby.

Tested on the following Rubies: MRI 2.2.1, 2.0.0, 1.9.3

Installation

Add this line to your application's Gemfile:

gem 'ioc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ioc

Usage

Block Registration

This example illustrates how to register a service/component with the container by passing a block to the register method.

require "ioc"

class AccountRepository
  def initialize(database, logger)
    @database = database
    @logger = logger
  end
  # ... some store & finder methods here
end

class UserRepository
  def initialize(database, logger)
    @database = database
    @logger = logger
  end
  # ... some store & finder methods here
end

class CreateAccountService
  def initialize(account_repository, user_repository, logger)
    @account_repository = account_repository
    @user_repository = user_repository
    @logger = logger
  end
  
  def call(req, res)
    # ... create account code
  end
end

# Register Services
container = IOC::Container.new
container.register(:create_account_service) do |c|
  CreateAccountService.new(c.account_repository, c.user_repository, c.logger)
end
container.register(:user_repository) do |c|
  UserRepository.new(c.database, c.logger)
end
container.register(:account_repository) do |c|
  AccountRepository.new(c.database, c.logger)
end
container.register(:logger){|c| Logger.new}
container.register(:database){|c| DB}

# Resolve Service
container.resolve(:create_account_service).call(req, res)

# Release the object graph for garbage collection
container.release

Automatic Dependency Resolution

Rather than assigning each service's dependencies manually like in the example above, we can allow the container to build and inject each service's dependencies automatically.

# Register Services
container = IOC::Container.new
container.register(:create_account_service, CreateAccountService)
container.register(:user_repository, UserRepository)
container.register(:account_repository, AccountRepository)
container.register(:logger){|c| Logger.new}
container.register(:database){|c| DB}

# Resolve Service
container.resolve(:create_account_service).call(req, res)

# Release the object graph for garbage collection
container.release

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

FAQs

Package last updated on 10 Nov 2015

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