iafonov / shit-brix Goto Github PK
View Code? Open in Web Editor NEWSimple DI container for ruby
Simple DI container for ruby
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.