Module | Dissident |
In: |
dissident.rb
dissident/lifecycle.rb |
INJECTED | = | {} | A hash containing classes to inject in as keys. | |
LIBRARIES | = | {} | A hash mapping classes to the libraries they belong to. | |
DEPENDENCIES | = | Hash.new { |h, k| h[k] = Hash.new { |l, m| l[m] = [] } | A hash mapping classes to their dependencies. |
Return the declared dependencies of klass that are to be injected using Constructor Injection.
# File dissident.rb, line 155 155: def constructor_parameters(klass) 156: if klass.const_defined? :DISSIDENT_CONSTRUCTOR 157: container = container_for klass 158: klass.const_get(:DISSIDENT_CONSTRUCTOR).map { |service| 159: container.fetch service 160: } 161: else 162: # By default, inject no constructor parameters. 163: [] 164: end 165: end
Set the global default container to default. Usually not needed, it is better style to use Dissident.with.
# File dissident.rb, line 77 77: def container=(default) 78: Thread.main[:DISSIDENT_CONTAINER][nil] = if default.nil? 79: nil 80: else 81: Dissident.instantiate default 82: end 83: end
Return the container that will be used for injecting dependencies into objects of klass.
# File dissident.rb, line 169 169: def container_for(klass) 170: copy_binding 171: Thread.current[:DISSIDENT_CONTAINER][library(klass)] 172: end
Return the dependencies klass has.
# File dissident/lifecycle.rb, line 32 32: def self.dependencies_for(klass) 33: Dissident::DEPENDENCIES[klass] 34: end
Inject all the dependencies of object.
# File dissident.rb, line 138 138: def inject(object) 139: if INJECTED[object.class] 140: container = container_for object.class 141: 142: if container.nil? 143: warn "Dissident: Cannot inject to #{object} " << 144: "(#{library(object) || "default application"}), " << 145: "no container given." 146: else 147: object.instance_variable_set :@__dissident__, container 148: end 149: end 150: object 151: end
Instantiate object (a Dissident::Container) to be used for dependency injection.
# File dissident.rb, line 87 87: def instantiate(object) 88: if object.kind_of? Class and object < Container 89: cont = object.new 90: cache = Cache.new(cont) 91: cont.cache = cache 92: else 93: raise ArgumentError, "#{object} is not a Dissident::Container" 94: end 95: end
Register klass to make use of Dissident.
This needs only to be explicitly called when you declare Constructor Injection with DISSIDENT_CONSTRUCTOR.
It is recommended to make use of provide instead of this.
# File dissident.rb, line 133 133: def use_for(klass) 134: klass.use_dissident! 135: end
Make cont the current global container in block.
Optionally, pass a hash that maps libraries to the container used for them. Example:
Dissident.with MyGlobalContainer, MyFirstLib => FirstContainer, MySecondLib => SecondContainer do ... end
# File dissident.rb, line 107 107: def with(cont, library = nil, &block) 108: if cont.kind_of? Hash or library.kind_of? Hash 109: if library.nil? 110: recurse cont, &block 111: else 112: with(cont) { recurse library, &block } 113: end 114: else 115: if block.nil? 116: raise ArgumentError, "Dissident.with must be called with a block" 117: end 118: 119: cont = instantiate cont 120: 121: fluid_let(library, cont) { 122: yield cont 123: } 124: end 125: end