Skip to content

iafonov/shit-brix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simple DI container for ruby. Mostly inspired by Guice. View test/spec_test.rb and linked samples for basic usage.

Here is simple example describing basic DI concepts, and demonstrates ShitBrix container usage techniques
class Service
  def go()
    # do something useful
    "work"
  end
end

class Action
  # To inject dependency call inject method, this method is automatically added
  # to all classes of your application, when the object will be instantiated through
  # ShitBrix.get_instance(:dep) call (or as nested dependency in hierarchy) instance 
  # variable will be created automatically
  inject :service  

  def perform()
    # You don't need to create instance variable explicitly, ShitBrix will do it
    # for you
    @service.go
  end
end

class NestedModule < AbstractModule
  def configure()    
    # 
  end
end

# In implementations of AbstractModule you should configure your dependencies,
# and bind them to initializers
class ApplicationModule < AbstractModule
  def configure()
    # By calling the install method and passing another AbstractModule
    # implementation you can divide configuration to several modules (different
    # classes implementing AbstractModule to avoid big and messy configure methods.
    # Modules can have unlimited level of nesting.
    install(NestedModule)
    
    # You can bind dependencies to blocks that instantiate them, ShitBrix uses
    # lazy initialization, so this block will be called when root class is
    # instantiated, and result of its execution will be injected to target class
    bind(:service).to{ Service.new }

    # You can bind dependencies to classes, class constructor (only default for
    # now) will be called on every injection, all requested dependencies will be 
    # injected
    bind(:action).to(Action)

    # You can bind dependency to class and add scope for class. As_singleton
    # tells ShitBrix to use only one instance for application. Constructor will
    # be called only once, on the first injection (lazy initialization)
    bind(:singleton_service).to(Service).as_singleton

    # As_eager_singleton modifier works same as as_singleton, but object will be
    # constructed immediately after bind. (eager initialization)    
    bind(:another_singleton_service).to(Service).as_eager_singleton

    # You can bind dependencies directly to objects and they will be directly
    # injected in correct place
    object = Object.new
    bind(:object).to(object)
  end
end

class Application
  def initialize
    # In the root 'initialize' method of your application you should init
    # container and pass AbstractModule implementation to it. 
    ShitBrix.init(ApplicationModule.new)

    # You should have only one explicit call of ShitBrix.get_instance
    # in your application. You should explicitly instantiate only root component of
    # your application and all dependencies will be injected to it hierarchically
    @action = ShitBrix.get_instance(:action)
  end

  def do_real_work    
    @action.perform
  end
end

About

Simple DI container for ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages