Class: IocRb::Container
Overview
IocRb::Container is the central data store for registering objects used for dependency injection. Users register classes by providing a name and a class to create the object(we call them beans). Beans may be retrieved by asking for them by name (via the [] operator)
Constant Summary collapse
- DEFAULT_CONST_LOADER =
IocRb::ConstLoaders::Native
Class Method Summary collapse
-
.new_with_beans(resources, const_loader = DEFAULT_CONST_LOADER) ⇒ Object
Evaluates the given array of blocks on the container instance what adds new bean definitions to the container.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns bean instance from the container by the specified bean name.
-
#bean(bean_name, options, &block) ⇒ Object
Registers new bean in container.
-
#eager_load_bean_classes ⇒ Object
Load defined in bean classes this is needed for production usage for eager loading.
-
#initialize(const_loader = DEFAULT_CONST_LOADER, &block) ⇒ Container
constructor
Constructor.
-
#replace_bean(bean_name, options, &block) ⇒ Object
Registers new bean in container and replace existing instance if it’s instantiated.
Constructor Details
#initialize(const_loader = DEFAULT_CONST_LOADER, &block) ⇒ Container
Constructor
20 21 22 23 24 25 26 |
# File 'lib/ioc_rb/container.rb', line 20 def initialize(const_loader = DEFAULT_CONST_LOADER, &block) @const_loader = const_loader @beans_metadata_storage = IocRb::BeansMetadataStorage.new @bean_factory = IocRb::BeanFactory.new(const_loader, @beans_metadata_storage) block.call(self) if block_given? end |
Class Method Details
.new_with_beans(resources, const_loader = DEFAULT_CONST_LOADER) ⇒ Object
Evaluates the given array of blocks on the container instance what adds new bean definitions to the container
31 32 33 34 35 36 37 38 39 |
# File 'lib/ioc_rb/container.rb', line 31 def self.new_with_beans(resources, const_loader = DEFAULT_CONST_LOADER) IocRb::ArgsValidator.is_array!(resources, :resources) self.new(const_loader).tap do |container| resources.each do |resource| resource.call(container) end end end |
Instance Method Details
#[](name) ⇒ Object
Returns bean instance from the container by the specified bean name
71 72 73 74 |
# File 'lib/ioc_rb/container.rb', line 71 def [](name) IocRb::ArgsValidator.is_symbol!(name, :bean_name) @bean_factory.get_bean(name) end |
#bean(bean_name, options, &block) ⇒ Object
Registers new bean in container
46 47 48 49 50 51 52 |
# File 'lib/ioc_rb/container.rb', line 46 def bean(bean_name, , &block) IocRb::ArgsValidator.is_symbol!(bean_name, :bean_name) IocRb::ArgsValidator.is_hash!(, :options) bean = IocRb::BeanMetadata.new(bean_name, , &block) @beans_metadata_storage.put(bean) end |
#eager_load_bean_classes ⇒ Object
Load defined in bean classes this is needed for production usage for eager loading
79 80 81 82 83 84 85 |
# File 'lib/ioc_rb/container.rb', line 79 def eager_load_bean_classes @beans_metadata_storage.bean_classes.each do |bean_class| if !bean_class.is_a?(Class) @const_loader.load_const(bean_class) end end end |
#replace_bean(bean_name, options, &block) ⇒ Object
Registers new bean in container and replace existing instance if it’s instantiated
59 60 61 62 63 64 65 |
# File 'lib/ioc_rb/container.rb', line 59 def replace_bean(bean_name, , &block) bean(bean_name, , &block) if @bean_factory.get_bean(bean_name) @bean_factory.delete_bean(bean_name) end end |