Giter Club home page Giter Club logo

shit-brix's Introduction

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

shit-brix's People

Contributors

iafonov avatar

Stargazers

Angus H. avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

dimiro1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.