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.
-
#initialize(const_loader = DEFAULT_CONST_LOADER, &block) ⇒ Container
constructor
Constructor.
Constructor Details
#initialize(const_loader = DEFAULT_CONST_LOADER, &block) ⇒ Container
Constructor
20 21 22 23 24 25 |
# File 'lib/ioc_rb/container.rb', line 20 def initialize(const_loader = DEFAULT_CONST_LOADER, &block) @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
30 31 32 33 34 35 36 37 38 |
# File 'lib/ioc_rb/container.rb', line 30 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
57 58 59 60 |
# File 'lib/ioc_rb/container.rb', line 57 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
45 46 47 48 49 50 51 |
# File 'lib/ioc_rb/container.rb', line 45 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 |