Class | Class |
In: |
lib/nukumi2/vendor/dissident.rb
|
Parent: | Object |
new | -> | __new_dissident__ |
Declare default (a Dissident::Container) to be used as container if none was declared dynamically. Only works with a proper library declaration for the class.
The global default container can be set with Dissident.container=.
# File lib/nukumi2/vendor/dissident.rb, line 59 59: def default_container(default) 60: lib = Dissident::LIBRARIES[self] 61: if lib.nil? 62: raise ArgumentError, "trying to set global default container inside class" 63: end 64: Thread.main[:DISSIDENT_CONTAINER][lib] = Dissident.instantiate default 65: end
Declare names to be dependencies of this class that should be injected at instantiation.
# File lib/nukumi2/vendor/dissident.rb, line 36 36: def inject(*names) 37: use_dissident! 38: # Mark for container injection. 39: Dissident::INJECTED[self] = true 40: names.each { |name| 41: define_method(name) { |*args| 42: @__dissident__.fetch name, *args 43: } 44: } 45: end
Declare this class to belong to the library lib (which is usually the namespace module of the library, or the core class).
# File lib/nukumi2/vendor/dissident.rb, line 49 49: def library(lib) 50: Dissident::LIBRARIES[self] = lib 51: end
Register the class in Dissident and replace the instantiation method new with magic to automatically inject the dependencies.
# File lib/nukumi2/vendor/dissident.rb, line 19 19: def use_dissident! 20: klass = self 21: unless Dissident::INJECTED.include? klass 22: Dissident::INJECTED[klass] = false 23: (class << self; self; end).module_eval { 24: define_method(:new) { |*args| 25: constructor_parameters = Dissident.constructor_parameters klass 26: object = __new_dissident__(*(constructor_parameters + args)) 27: Dissident.inject object 28: object 29: } 30: } 31: end 32: end