module Dry::DependencyInjection::Singletons
Public Class Methods
extended(base)
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 10 def self.extended(base) hooks_mod = ::Module.new do def inherited(subclass) subclass.instance_variable_set(:@_lock, Mutex.new) subclass.instance_variable_set(:@_lock, @_eager.dup) super end end base.class_eval do extend Dry::Container::Mixin extend hooks_mod setting :lazy, true setting :registry, Registry.new class << self def register(*args) register_singleton(*args) end end @_eager = [] @_lock = Mutex.new end end
included(base)
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 44 def self.included(base) base.class_eval do include ::Dry::Container::Mixin prepend Initializer setting :lazy, true setting :registry, Registry.new def register(*args) register_singleton(*args) end end end
register(*args)
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 26 def register(*args) register_singleton(*args) end
Public Instance Methods
finalize()
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 58 def finalize @_lock.synchronize do if config.lazy finalize_lazy else finalize_eager end @_finalized = ! config.lazy end end
inherited(subclass)
click to toggle source
Calls superclass method
# File lib/dry/dependency_injection/singletons.rb, line 12 def inherited(subclass) subclass.instance_variable_set(:@_lock, Mutex.new) subclass.instance_variable_set(:@_lock, @_eager.dup) super end
register(*args)
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 52 def register(*args) register_singleton(*args) end
Private Instance Methods
check_finalized()
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 84 def check_finalized if @_finalized raise Dry::Container::Error, 'can not register. container already finalized' end end
finalize_eager()
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 90 def finalize_eager keys.each do |key| self[key] end end
finalize_lazy()
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 96 def finalize_lazy @_eager.each do |key| self[key] end end
register_singleton(key, item = nil, options = {}, &block)
click to toggle source
# File lib/dry/dependency_injection/singletons.rb, line 71 def register_singleton(key, item = nil, options = {}, &block) @_lock.synchronize do check_finalized end if block_given? raise Dry::Container::Error, 'can not register block' else config.registry.call(_container, key, item, options) @_eager << key if options[:eager] || item.kind_of?(Eager) self end end