class Dry::DependencyInjection::Item

Public Class Methods

new(lock, clazz) click to toggle source
# File lib/dry/dependency_injection/item.rb, line 5
def initialize(lock, clazz)
  unless clazz.is_a?(Class)
    raise Dry::Container::Error, "needs to be Class object: #{clazz}"
  end
  @_lock = lock
  @clazz = clazz
  @circular = false
end

Public Instance Methods

call() click to toggle source
# File lib/dry/dependency_injection/item.rb, line 14
def call
  @_lock.acquire_read_lock
  unless @instance
    @instance = create
  end
  @instance
ensure
  @_lock.release_read_lock
end
create() click to toggle source
# File lib/dry/dependency_injection/item.rb, line 24
def create
  @_lock.acquire_write_lock
  begin
    if @circular
      raise Dry::Container::Error, "circular dependency detected: #{@clazz} depends on itself"
    end
    @circular = true
    @clazz.new
  ensure
    @_lock.release_write_lock
  end
end