Class: Rioc::RiocContainer

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

Overview

Rioc container class that is responsible for storing all the beans definitions as well as live instance of the beans declared.

Instance Method Summary collapse

Constructor Details

#initializeRiocContainer

Initialize the IoC container



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

def initialize
  @container = {}
  @beans = {}
  @in_recursion = false

  # Used to resolve dependencies
  @visited = Set.new
  @built = Set.new
end

Instance Method Details

#build_containerObject

Build container



49
50
51
52
53
# File 'lib/rioc/container.rb', line 49

def build_container
  @beans
    .reject { |name| @beans[name].lazy }
    .each { |name, _| resolve(name) }
end

#register(name, scope: Rioc::Bean::Scope::SINGLETON, lazy: false, &block) ⇒ Object

Register a instance without any need of resolving dependencies



23
24
25
26
27
28
29
# File 'lib/rioc/container.rb', line 23

def register(name, scope: Rioc::Bean::Scope::SINGLETON, lazy: false, &block)
  @beans[name] = Rioc::Bean::RiocBean.new(name,
                                          Rioc::Bean::BeanFactory.new(self, name, block),
                                          scope,
                                          lazy)

end

#resolve(name) ⇒ Object

Resolve bean with the provided bean name

Parameters:

  • name
    • The name of the bean to resolve

Returns:

  • A live instance of the bean definition

Raises:

  • (UnknownDependencyNameError)


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

def resolve(name)
  # Should panic if the bean name is never registered
  raise UnknownDependencyNameError, name unless @beans[name]

  bean = @beans[name]

  # If the bean already exists in the container and the scope is singleton,
  # directly return the bean instance
  return @container[name] if @container[name] && bean.scope == Rioc::Bean::Scope::SINGLETON

  # Call the internal function to create the bean instance and return it
  resolve_bean(name)
end

#start_applicationObject

Start application



56
# File 'lib/rioc/container.rb', line 56

def start_application; end