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

smart_ioc

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

smart_ioc

  • 0.5.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

SmartIoC

Rspec Code Climate

SmartIoC is a smart and really simple IoC container for Ruby applications.

Installation

gem install smart_ioc

Ruby versions

Please install specific smart_ioc version, depending on Ruby version.

Ruby VersionSmartIoC Version
< 3.00.3.2
>= 3.00.4.0

Setup

Set package name and source package folder with beans. SmartIoC will parse source files and detect bean definitions automatically for you.

SmartIoC.find_package_beans(:PACKAGE_NAME, File.dirname(__FILE__))

If you have several packages in your application (like if you are using rdm package manager) you can run SmartIoC.find_package_beans several time pointing it to the source folder and setting a different package name.

Basic information

  1. Different packages can use beans with same name.
  2. For a specific package you can declare beans with same name if they have different context.
class UsersRepository
  include SmartIoC::Iocify
  bean :users_repository
end

class Test::UsersRepository
  include SmartIoC::Iocify
  bean :users_repository, context: :test
end
  1. You can extend the :default context with any other in the following way:
SmartIoC::Container.get_instance.set_extra_context_for_package(:YOUR_PACKAGE_NAME, :test)

This allows to create test implementations for any package dependency.

  1. In order to get a bean use SmartIoC::Container.get_bean(:BEAN_NAME, package: :PACKAGE_NAME, context: :default). package and context are optional arguments.

  2. If you use the same bean name for different dependencies in different packages you will need to specify the package directly. You can do that by using from parameter:

class UsersCreator
  include SmartIoC::Iocify
  bean :users_creator

  inject :users_repository, from: :repositories

  def create
    user = User.new
    users_repository.put(user)
  end

end
  1. To have a diffent local name for a specific bean use the ref parameter. In the following example we are injecting the :users_repository dependency but refer to it as repo locally.
class UsersCreator
  include SmartIoC::Iocify
  bean :users_creator

  inject :users_repository, ref: :repo, from: :repositories

  def create
    user = User.new
    repo.put(user)
  end
end
  1. Use factory method to instantiate the bean via a special creational method
class RepositoryFactory
  include SmartIoC::Iocify
  bean :users_creator, factory_method: :get_bean

  inject :config
  inject :users_repository
  inject :admins_repository

  def get_bean
    if config.admin_access?
      admins_repository
    else
      users_repository
    end
  end

  def create
    user = User.new
    repo.put(user)
  end
end
  1. Class level beans (object will not be instantiated and class will be used for that bean instead). Set instance: false:
class UsersCreator
  include SmartIoC::Iocify
  bean :users_creator, instance: false

  inject :users_repository
end

FAQs

Package last updated on 07 Oct 2024

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