Class: Callme::Container

Inherits:
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 beans). Beans 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 beans definitions

  • &block (Proc)

    optional proc with container’s beans definitions



20
21
22
23
24
25
26
# File 'lib/callme/container.rb', line 20

def initialize(const_loader = DEFAULT_CONST_LOADER, &block)
  @const_loader           = const_loader
  @beans_metadata_storage = Callme::BeansMetadataStorage.new
  @bean_factory           = Callme::BeanFactory.new(const_loader, @beans_metadata_storage)

  block.call(self) if block_given?
end

Class Method Details

.new_with_beans(resources, const_loader = DEFAULT_CONST_LOADER) ⇒ Object

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

Parameters:

  • resources (Array)

    array of procs with container’s beans definitions



31
32
33
34
35
36
37
38
39
# File 'lib/callme/container.rb', line 31

def self.new_with_beans(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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/callme/container.rb', line 41

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

Instance Method Details

#[](name) ⇒ Object

Returns bean instance from the container by the specified bean name

Parameters:

  • name (Symbol)

    bean name

Returns:

  • bean instance



86
87
88
89
# File 'lib/callme/container.rb', line 86

def [](name)
  Callme::ArgsValidator.is_symbol!(name, :bean_name)
  return @bean_factory.get_bean(name)
end

#bean(bean_name, options, &block) ⇒ Object

Registers new bean in container

Parameters:

  • bean_name (Symbol)

    bean name

  • options (Hash)

    includes bean class and bean scope

  • &block (Proc)

    the block which describes bean dependencies, see more in the BeanMetadata



58
59
60
61
62
63
64
# File 'lib/callme/container.rb', line 58

def bean(bean_name, options, &block)
  Callme::ArgsValidator.is_symbol!(bean_name, :bean_name)
  Callme::ArgsValidator.is_hash!(options, :options)

  bean = Callme::BeanMetadata.new(bean_name, options, &block)
  @beans_metadata_storage.put(bean)
end

#eager_load_bean_classesObject

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



98
99
100
101
102
103
104
# File 'lib/callme/container.rb', line 98

def eager_load_bean_classes
  @beans_metadata_storage.bean_classes.each do |bean_class|
    if !bean_class.is_a?(Class)
      @const_loader.load_const(bean_class)
    end
  end
end

#keysObject



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

def keys
  @beans_metadata_storage.keys
end

#replace_bean(bean_name, options, &block) ⇒ Object

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

Parameters:

  • bean_name (Symbol)

    bean name

  • options (Hash)

    includes bean class and bean scope

  • &block (Proc)

    the block which describes bean dependencies, see more in the BeanMetadata



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

def replace_bean(bean_name, options, &block)
  if @bean_factory.get_bean(bean_name)
    @bean_factory.delete_bean(bean_name)
  end
  bean(bean_name, options, &block)
end

#reset!Object



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

def reset!
  @bean_factory = Callme::BeanFactory.new(@const_loader, @beans_metadata_storage)
end