Class: Rioc::RiocContainer
- Inherits:
-
Object
- Object
- Rioc::RiocContainer
- 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
-
#build_container ⇒ Object
Build container.
-
#initialize ⇒ RiocContainer
constructor
Initialize the IoC container.
-
#register(name, scope: Rioc::Bean::Scope::SINGLETON, lazy: false, &block) ⇒ Object
Register a instance without any need of resolving dependencies.
-
#resolve(name) ⇒ Object
Resolve bean with the provided bean name.
-
#start_application ⇒ Object
Start application.
Constructor Details
#initialize ⇒ RiocContainer
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_container ⇒ Object
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
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_application ⇒ Object
Start application
56 |
# File 'lib/rioc/container.rb', line 56 def start_application; end |