Sha256: b78b0dd4123073719b848421f358b18baada760d54d019112b0c287416e2a8c2

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module SmartCore::Container::Entities
  # @api private
  # @since 0.2.0
  # @version 0.10.0
  class MemoizedDependency < Dependency
    # @param dependency_name [String]
    # @param dependency_definition [Proc]
    # @return [void]
    #
    # @api private
    # @since 0.2.0
    # @version 0.10.0
    def initialize(dependency_name, dependency_definition)
      super(dependency_name, dependency_definition)
      @lock = SmartCore::Engine::ReadWriteLock.new
    end

    # @param host_container [SmartCore::Container, NilClass]
    # @return [Any]
    #
    # @api private
    # @since 0.2.0
    # @version 0.8.1
    def reveal(host_container = SmartCore::Container::NO_HOST_CONTAINER)
      @lock.read_sync do
        unless instance_variable_defined?(:@revealed_dependency)
          @revealed_dependency = dependency_definition.call
        else
          @revealed_dependency
        end
      end
    end

    private

    # @return [Proc]
    #
    # @api private
    # @since 0.2.0
    attr_reader :dependency_definition
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_container-0.11.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.10.0 lib/smart_core/container/entities/memoized_dependency.rb