Class: SmartIoC::Container
- Inherits:
-
Object
- Object
- SmartIoC::Container
- Includes:
- Args
- Defined in:
- lib/smart_ioc/container.rb
Overview
SmartIoC::Container is a beans store used for dependency injection
Constant Summary collapse
- DEFAULT_CONTEXT =
:default
Class Method Summary collapse
Instance Method Summary collapse
- #clear_scopes ⇒ Object
-
#find_bean_definition(bean_name, package, context) ⇒ Object
Returns bean definition for specific class return [BeanDefinition].
- #force_clear_scopes ⇒ Object
-
#get_bean(bean_name, package: nil, context: nil, parent_bean_definition: nil, parent_bean_name: nil) ⇒ Object
Bean instance from container.
- #get_bean_definition ⇒ Object
-
#initialize ⇒ Container
constructor
A new instance of Container.
-
#register_bean(bean_name:, klass:, context:, scope:, path:, after_init:, factory_method: nil, package_name: nil, instance: true) ⇒ SmartIoC::BeanDefinition
Bean definition.
- #require_bean(bean_name) ⇒ Any
-
#set_extra_context_for_package(package_name, context) ⇒ Object
Sets extra context for specific package.
-
#set_load_proc(&proc) ⇒ Object
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.
-
#unregister_bean(bean_definition) ⇒ Object
Nil.
Methods included from Args
#check_arg, #check_arg_any, #not_nil
Constructor Details
#initialize ⇒ Container
Returns a new instance of Container.
18 19 20 |
# 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 |
Class Method Details
.clear ⇒ Object
13 14 15 |
# File 'lib/smart_ioc/container.rb', line 13 def clear @container = nil end |
Instance Method Details
#clear_scopes ⇒ Object
134 135 136 |
# File 'lib/smart_ioc/container.rb', line 134 def clear_scopes bean_factory.clear_scopes end |
#find_bean_definition(bean_name, package, context) ⇒ Object
Returns bean definition for specific class return [BeanDefinition]
93 94 95 |
# File 'lib/smart_ioc/container.rb', line 93 def find_bean_definition(bean_name, package, context) bean_definitions_storage.find_bean(bean_name, package, context) end |
#force_clear_scopes ⇒ Object
138 139 140 141 |
# File 'lib/smart_ioc/container.rb', line 138 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) ⇒ Object
Returns bean instance from container.
124 125 126 127 128 129 130 131 132 |
# File 'lib/smart_ioc/container.rb', line 124 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 ⇒ Object
97 98 99 |
# File 'lib/smart_ioc/container.rb', line 97 def get_bean_definition(...) bean_factory.get_bean_definition(...) end |
#register_bean(bean_name:, klass:, context:, scope:, path:, after_init:, factory_method: nil, package_name: nil, instance: true) ⇒ SmartIoC::BeanDefinition
Returns bean definition.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# 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) ⇒ Any
145 146 147 |
# File 'lib/smart_ioc/container.rb', line 145 def require_bean(bean_name) bean_factory.bean_file_loader.require_bean(bean_name) end |
#set_extra_context_for_package(package_name, context) ⇒ Object
Sets extra context for specific package
114 115 116 117 |
# File 'lib/smart_ioc/container.rb', line 114 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) ⇒ Object
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
107 108 109 |
# File 'lib/smart_ioc/container.rb', line 107 def set_load_proc(&proc) bean_factory.bean_file_loader.set_load_proc(&proc) end |
#unregister_bean(bean_definition) ⇒ Object
Returns nil.
24 25 26 27 28 |
# File 'lib/smart_ioc/container.rb', line 24 def unregister_bean(bean_definition) bean_definitions_storage.delete(bean_definition) clear_scopes nil end |