Sha256: 1d4c57283469bd306253f9a74aff98bf110cc886e15d7538a62d842119767335

Contents?: true

Size: 894 Bytes

Versions: 6

Compression:

Stored size: 894 Bytes

Contents

# frozen_string_literal: true

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

    # @return [Any]
    #
    # @api private
    # @since 0.2.0
    def reveal
      @lock.thread_safe 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

6 entries across 6 versions & 1 rubygems

Version Path
smart_container-0.7.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.6.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.5.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.4.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.3.0 lib/smart_core/container/entities/memoized_dependency.rb
smart_container-0.2.0 lib/smart_core/container/entities/memoized_dependency.rb