-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
82 lines (69 loc) · 2.96 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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