class Injectable::DependenciesProxy

Memoizes the Dependencies generated by Dependency based on DependencyGraph

Attributes

graph[R]
namespace[R]

Public Class Methods

new(graph:, namespace: nil) click to toggle source
# File lib/injectable/dependencies_proxy.rb, line 6
def initialize(graph:, namespace: nil)
  @graph = graph
  @namespace = namespace
  @instances = {}
end

Public Instance Methods

get(name) click to toggle source

Get the instance of the dependency name

# File lib/injectable/dependencies_proxy.rb, line 13
def get(name)
  @instances[name] ||= graph[name].instance(args: memoized_dependencies_of(name), namespace: namespace)
end

Private Instance Methods

dependencies_of(name) click to toggle source
# File lib/injectable/dependencies_proxy.rb, line 25
def dependencies_of(name)
  graph[name].depends_on
end
memoized_dependencies_of(name) click to toggle source
# File lib/injectable/dependencies_proxy.rb, line 19
def memoized_dependencies_of(name)
  return [] if dependencies_of(name).empty?

  dependencies_of(name).each_with_object({}) { |dep, hash| hash[dep] = get(dep) }
end