Class: Callme::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/callme/container.rb

Overview

Callme::Container is the central data store for registering objects used for dependency injection. Users register classes by providing a name and a class to create the object(we call them deps). Deps may be retrieved by asking for them by name (via the [] operator)

Constant Summary collapse

DEFAULT_CONST_LOADER =
Callme::ConstLoaders::Native

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(const_loader = DEFAULT_CONST_LOADER, &block) ⇒ Container

Constructor

Parameters:

  • resources (Array)

    array of procs with container’s deps definitions

  • &block (Proc)

    optional proc with container’s deps definitions



13
14
15
16
17
18
19
# File 'lib/callme/container.rb', line 13

def initialize(const_loader = DEFAULT_CONST_LOADER, &block)
  @const_loader          = const_loader
  @deps_metadata_storage = Callme::DepsMetadataStorage.new
  @dep_factory           = Callme::DepFactory.new(const_loader, @deps_metadata_storage)

  block.call(self) if block_given?
end

Class Method Details

.new_with_deps(resources, const_loader = DEFAULT_CONST_LOADER) ⇒ Object

Evaluates the given array of blocks on the container instance what adds new dep definitions to the container

Parameters:

  • resources (Array)

    array of procs with container’s deps definitions



24
25
26
27
28
29
30
31
32
# File 'lib/callme/container.rb', line 24

def self.new_with_deps(resources, const_loader = DEFAULT_CONST_LOADER)
  Callme::ArgsValidator.is_array!(resources, :resources)

  self.new(const_loader).tap do |container|
    resources.each do |resource|
      resource.call(container)
    end
  end
end

.with_parent(parent_container, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/callme/container.rb', line 34

def self.with_parent(parent_container, &block)
  const_loader          = parent_container.instance_variable_get("@const_loader")
   = parent_container.instance_variable_get("@deps_metadata_storage").copy
  container             = self.new(const_loader)
  container.instance_eval do
    @deps_metadata_storage = 
    @dep_factory           = Callme::DepFactory.new(const_loader, )
  end
  block.call(container) if block_given?
  container
end

Instance Method Details

#[](name) ⇒ Object

Returns dep instance from the container by the specified dep name

Parameters:

  • name (Symbol)

    dep name

Returns:

  • dep instance



79
80
81
82
# File 'lib/callme/container.rb', line 79

def [](name)
  Callme::ArgsValidator.is_symbol!(name, :dep_name)
  return @dep_factory.get_dep(name)
end

#dep(dep_name, options, &block) ⇒ Object

Registers new dep in container

Parameters:

  • dep_name (Symbol)

    dep name

  • options (Hash)

    includes dep class and dep scope

  • &block (Proc)

    the block which describes dep dependencies, see more in the DepMetadata



51
52
53
54
55
56
57
# File 'lib/callme/container.rb', line 51

def dep(dep_name, options, &block)
  Callme::ArgsValidator.is_symbol!(dep_name, :dep_name)
  Callme::ArgsValidator.is_hash!(options, :options)

  dep = Callme::DepMetadata.new(dep_name, options, &block)
  @deps_metadata_storage.put(dep)
end

#eager_load_dep_classesObject

Load defined in dep classes this is needed for production usage for eager loading



91
92
93
94
95
96
# File 'lib/callme/container.rb', line 91

def eager_load_dep_classes
  @deps_metadata_storage.values.each do ||
    @const_loader.load_const(.dep_class)
    @const_loader.load_const(.contract) if .contract
  end
end

#keysObject



84
85
86
# File 'lib/callme/container.rb', line 84

def keys
  @deps_metadata_storage.keys
end

#replace_dep(dep_name, options, &block) ⇒ Object

Registers new dep in container and replace existing instance if it’s instantiated

Parameters:

  • dep_name (Symbol)

    dep name

  • options (Hash)

    includes dep class and dep scope

  • &block (Proc)

    the block which describes dep dependencies, see more in the DepMetadata



64
65
66
67
68
69
# File 'lib/callme/container.rb', line 64

def replace_dep(dep_name, options, &block)
  if @dep_factory.get_dep(dep_name)
    @dep_factory.delete_dep(dep_name)
  end
  dep(dep_name, options, &block)
end

#reset!Object



71
72
73
# File 'lib/callme/container.rb', line 71

def reset!
  @dep_factory = Callme::DepFactory.new(@const_loader, @deps_metadata_storage)
end