class SmartIoC::Container

SmartIoC::Container is a beans store used for dependency injection

Constants

DEFAULT_CONTEXT

Public Class Methods

clear() click to toggle source
# File lib/smart_ioc/container.rb, line 13
def clear
  @container = nil
end
get_instance() click to toggle source
# File lib/smart_ioc/container.rb, line 9
def get_instance
  @container ||= SmartIoC::Container.allocate
end
new() click to toggle source
# File lib/smart_ioc/container.rb, line 18
def initialize
  raise ArgumentError, "SmartIoC::Container should not be allocated. Use SmartIoC::Container.get_instance instead"
end

Public Instance Methods

clear_scopes() click to toggle source
# File lib/smart_ioc/container.rb, line 130
def clear_scopes
  bean_factory.clear_scopes
end
force_clear_scopes() click to toggle source
# File lib/smart_ioc/container.rb, line 134
def force_clear_scopes
  bean_factory.force_clear_scopes
  bean_factory.bean_file_loader.clear_locations
end
get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil) click to toggle source

@param bean_name [Symbol] bean name @param optional package [Symbol] package name @param optional parent_bean_definition [SmartIoc::BeanDefinition] bean definition of parent bean @param optional context [Symbol] package context @return bean instance from container

# File lib/smart_ioc/container.rb, line 120
def get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil)
  bean_factory.get_bean(
    bean_name,
    package: package,
    parent_bean_definition: parent_bean_definition,
    context: context,
    parent_bean_name: parent_bean_name,
  )
end
get_bean_definition(bean_name, package, context) click to toggle source

Returns bean definition for specific class @param bean_name [Symbol] @param package [Symbol] @param context [Symbol] return [BeanDefinition]

# File lib/smart_ioc/container.rb, line 93
def get_bean_definition(bean_name, package, context)
  bean_definitions_storage.find_bean(bean_name, package, context)
end
register_bean(bean_name:, klass:, context:, scope:, path:, after_init:, factory_method: nil, package_name: nil, instance: true) click to toggle source

@param bean_name [Symbol] bean name @param klass [Class] bean class name @param path [String] bean file absolute path @param scope [Symbol] scope value @param context [Symbol] bean context @param after_init [Symbol] name of bean method that will be called after bean initialization @return [SmartIoC::BeanDefinition] bean definition

# File lib/smart_ioc/container.rb, line 37
def register_bean(bean_name:, klass:, context:, scope:, path:, after_init:,
                  factory_method: nil, package_name: nil, instance: true)
  context ||= DEFAULT_CONTEXT

  check_arg(bean_name, :bean_name, Symbol)
  check_arg(context, :context, Symbol)
  check_arg(klass, :klass, Class)
  check_arg(path, :path, String)
  check_arg(factory_method, :factory_method, Symbol) if factory_method
  check_arg_any(instance, :instance, [TrueClass, FalseClass])

  scope ||= SmartIoC::Scopes::Singleton::VALUE

  allowed_scopes = [
    SmartIoC::Scopes::Prototype::VALUE,
    SmartIoC::Scopes::Singleton::VALUE,
    SmartIoC::Scopes::Request::VALUE
  ]

  if !allowed_scopes.include?(scope)
    raise ArgumentError, "bean scope should be one of #{allowed_scopes.inspect}"
  end

  if !package_name
    raise ArgumentError, %Q(
      Package name should be given for bean :#{bean_name}.
      You should specify package name directly or run

      SmartIoC.find_package_beans(package_name, dir)

      to setup beans before you actually register them.
    )
  end

  bean_definition = SmartIoC::BeanDefinition.new(
    name:           bean_name,
    package:        package_name,
    path:           path,
    klass:          klass,
    instance:       instance,
    factory_method: factory_method,
    context:        context,
    scope:          scope,
    after_init:     after_init,
  )

  bean_definitions_storage.push(bean_definition)

  bean_definition
end
require_bean(bean_name) click to toggle source

@param bean_name [Symbol] bean name @return [Any]

# File lib/smart_ioc/container.rb, line 141
def require_bean(bean_name)
  bean_factory.bean_file_loader.require_bean(bean_name)
end
set_extra_context_for_package(package_name, context) click to toggle source

Sets extra context for specific package @param package_name [Symbol] package name @param context [Symbol] context (ex: :test)

# File lib/smart_ioc/container.rb, line 110
def set_extra_context_for_package(package_name, context)
  extra_package_contexts.set_context(package_name, context)
  bean_definitions_storage.clear_dependencies
end
set_load_proc(&proc) click to toggle source

Sets new load proc for those who use active support dependency loader one can use SmartIoC.set_load_proc do |location|

require_dependency(location)

end

# File lib/smart_ioc/container.rb, line 103
def set_load_proc(&proc)
  bean_factory.bean_file_loader.set_load_proc(&proc)
end
unregister_bean(klass) click to toggle source

@param klass [Class] bean class name @return nil

# File lib/smart_ioc/container.rb, line 24
def unregister_bean(klass)
  bean_definitions_storage.delete_by_class(klass)
  clear_scopes
  nil
end

Private Instance Methods

bean_definitions_storage() click to toggle source
# File lib/smart_ioc/container.rb, line 155
def bean_definitions_storage
  @bean_definitions_storage ||= SmartIoC::BeanDefinitionsStorage.new
end
bean_factory() click to toggle source
# File lib/smart_ioc/container.rb, line 147
def bean_factory
  @bean_factory ||= SmartIoC::BeanFactory.new(bean_definitions_storage, extra_package_contexts)
end
extra_package_contexts() click to toggle source
# File lib/smart_ioc/container.rb, line 151
def extra_package_contexts
  @extra_package_contexts ||= SmartIoC::ExtraPackageContexts.new
end