class Rioc::RiocContainer

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

Public Class Methods

new() click to toggle source

Initialize the IoC container

# 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

Public Instance Methods

build_container() click to toggle source

Build container

# 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) click to toggle source

Register a instance without any need of resolving dependencies

# 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) click to toggle source

Resolve bean with the provided bean name @param name - The name of the bean to resolve @return A live instance of the bean definition

# 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() click to toggle source

Start application

# File lib/rioc/container.rb, line 56
def start_application; end

Private Instance Methods

build_bean(bean) click to toggle source

Internal method to instantiate a new bean

# File lib/rioc/container.rb, line 84
def build_bean(bean)
  @visited.add(bean)
  instance = @beans[bean].factory.build_instance
  @container[bean] = instance if @beans[bean].scope == Rioc::Bean::Scope::SINGLETON
  @built.add(bean)
  instance
end
clear_dfs_history() click to toggle source

Internal method to clear the history of the DFS search to resolve a bean

# File lib/rioc/container.rb, line 93
def clear_dfs_history
  @visited.clear
  @built.clear
end
resolve_bean(bean) click to toggle source

Internal method to resolve bean if the bean doesn't have an instance in container or the bean is transient.

# File lib/rioc/container.rb, line 62
def resolve_bean(bean)
  # We need to know if we are recursively resolving the dependencies
  # in order to detect cyclic dependencies
  unless @in_recursion
    @in_recursion = true
    clear_dfs_history

    instance = build_bean(bean)

    @in_recursion = false
    clear_dfs_history
    return instance
  end

  # Check if it has cyclic dependency
  raise Rioc::Errors::CyclicDependencyError, bean if @visited.include?(bean) && !@built.include?(bean)

  # Resolve current dependencies before building the instance
  build_bean(bean)
end