Class Class
In: dissident.rb
dissident/lifecycle.rb
Parent: Object

Dissident adds a few helper methods to Class.

Methods

Public Instance methods

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=.

[Source]

    # File dissident.rb, line 54
54:   def default_container(default)
55:     lib = Dissident::LIBRARIES[self]
56:     if lib.nil?
57:       raise ArgumentError, "trying to set global default container inside class"
58:     end
59:     Thread.main[:DISSIDENT_CONTAINER][lib] = Dissident.instantiate default
60:   end

Declare names to be dependencies of this class that should be injected at instantiation.

[Source]

    # File dissident.rb, line 31
31:   def inject(*names)
32:     use_dissident!
33:     # Mark for container injection.
34:     Dissident::INJECTED[self] = true
35:     names.each { |name|
36:       define_method(name) { |*args|
37:         @__dissident__.fetch name, *args
38:       }
39:     }
40:   end

Declare this class to belong to the library lib (which is usually the namespace module of the library, or the core class).

[Source]

    # File dissident.rb, line 44
44:   def library(lib)
45:     Dissident::LIBRARIES[self] = lib
46:   end

Register the class in Dissident and replace the instantiation method new with magic to automatically inject the dependencies.

[Source]

    # File dissident.rb, line 13
13:   def use_dissident!
14:     old_new = method :new
15:     klass = self
16:     unless Dissident::INJECTED.include? klass
17:       Dissident::INJECTED[klass] = false
18:       (class << self; self; end).instance_eval {
19:         define_method(:new) { |*args|
20:           constructor_parameters = Dissident.constructor_parameters klass
21:           object = old_new.call *(constructor_parameters + args)
22:           Dissident.inject object
23:           object
24:         }
25:       }
26:     end
27:   end

[Validate]