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
- #force_clear_scopes ⇒ Object
-
#get_bean(bean_name, package: nil, context: nil) ⇒ Object
Bean instance from container.
-
#get_bean_definition_by_class(klass) ⇒ Object
Returns bean definition for specific class return [BeanDefinition].
-
#initialize ⇒ Container
constructor
A new instance of Container.
-
#register_bean(bean_name:, klass:, context:, scope:, path:, factory_method: nil, package_name: nil, instance: true) ⇒ SmartIoC::BeanDefinition
Bean definition.
-
#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(klass) ⇒ Object
Nil.
Methods included from Args
#check_arg, #check_arg_any, #not_nil
Constructor Details
#initialize ⇒ Container
Returns a new instance of Container.
22 23 24 |
# File 'lib/smart_ioc/container.rb', line 22 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 |
.get_bean(bean_name, package: nil, context: nil) ⇒ Object
17 18 19 |
# File 'lib/smart_ioc/container.rb', line 17 def get_bean(bean_name, package: nil, context: nil) get_instance.get_bean(bean_name, package: package, context: context) end |
.get_instance ⇒ Object
9 10 11 |
# File 'lib/smart_ioc/container.rb', line 9 def get_instance @container ||= SmartIoC::Container.allocate end |
Instance Method Details
#clear_scopes ⇒ Object
125 126 127 |
# File 'lib/smart_ioc/container.rb', line 125 def clear_scopes bean_factory.clear_scopes end |
#force_clear_scopes ⇒ Object
129 130 131 132 |
# File 'lib/smart_ioc/container.rb', line 129 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) ⇒ Object
Returns bean instance from container.
121 122 123 |
# File 'lib/smart_ioc/container.rb', line 121 def get_bean(bean_name, package: nil, context: nil) bean_factory.get_bean(bean_name, package: package, context: context) end |
#get_bean_definition_by_class(klass) ⇒ Object
Returns bean definition for specific class return [BeanDefinition]
95 96 97 |
# File 'lib/smart_ioc/container.rb', line 95 def get_bean_definition_by_class(klass) bean_definitions_storage.find_by_class(klass) end |
#register_bean(bean_name:, klass:, context:, scope:, path:, factory_method: nil, package_name: nil, instance: true) ⇒ SmartIoC::BeanDefinition
Returns bean definition.
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 87 88 89 90 |
# File 'lib/smart_ioc/container.rb', line 40 def register_bean(bean_name:, klass:, context:, scope:, path:, 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 package_name ||= SmartIoC::BeanLocations.get_bean_package(path) 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 ) bean_definitions_storage.push(bean_definition) bean_definition end |
#set_extra_context_for_package(package_name, context) ⇒ Object
Sets extra context for specific package
112 113 114 115 |
# File 'lib/smart_ioc/container.rb', line 112 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
105 106 107 |
# File 'lib/smart_ioc/container.rb', line 105 def set_load_proc(&proc) bean_factory.bean_file_loader.set_load_proc(&proc) end |
#unregister_bean(klass) ⇒ Object
Returns nil.
28 29 30 31 32 |
# File 'lib/smart_ioc/container.rb', line 28 def unregister_bean(klass) bean_definitions_storage.delete_by_class(klass) clear_scopes nil end |