Class: IocRb::BeanFactory
Overview
Instantiates beans according to their scopes
Instance Attribute Summary collapse
-
#const_loader ⇒ Object
readonly
Returns the value of attribute const_loader.
Instance Method Summary collapse
-
#create_bean_and_save(bean_metadata, beans_storage) ⇒ Object
Create new bean instance according to the specified
bean_metadata. -
#get_bean(name) ⇒ Object
Get bean from the container by it’s
name. -
#get_bean_with_metadata(bean_metadata) ⇒ Object
Get bean by the specified bean metadata.
-
#initialize(const_loader, beans_metadata_storage) ⇒ BeanFactory
constructor
Constructor.
Constructor Details
#initialize(const_loader, beans_metadata_storage) ⇒ BeanFactory
Constructor
12 13 14 15 16 17 18 |
# File 'lib/ioc_rb/bean_factory.rb', line 12 def initialize(const_loader, ) @const_loader = const_loader @beans_metadata_storage = @singleton_scope = IocRb::Scopes::SingletonScope.new(self) @prototype_scope = IocRb::Scopes::PrototypeScope.new(self) @request_scope = IocRb::Scopes::RequestScope.new(self) end |
Instance Attribute Details
#const_loader ⇒ Object (readonly)
Returns the value of attribute const_loader.
8 9 10 |
# File 'lib/ioc_rb/bean_factory.rb', line 8 def const_loader @const_loader end |
Instance Method Details
#create_bean_and_save(bean_metadata, beans_storage) ⇒ Object
Create new bean instance according to the specified bean_metadata
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ioc_rb/bean_factory.rb', line 55 def create_bean_and_save(, beans_storage) if .bean_class.is_a?(Class) bean_class = .bean_class else bean_class = const_loader.load_const(.bean_class) .fetch_attrs!(bean_class) end bean = .instance ? bean_class.new : bean_class if .has_factory_method? set_bean_dependencies(bean, ) bean = bean.send(.factory_method) beans_storage[.name] = bean else # put to container first to prevent circular dependencies beans_storage[.name] = bean set_bean_dependencies(bean, ) end bean end |
#get_bean(name) ⇒ Object
Get bean from the container by it’s name. According to the bean scope it will be newly created or returned already instantiated bean
26 27 28 29 30 31 32 |
# File 'lib/ioc_rb/bean_factory.rb', line 26 def get_bean(name) = @beans_metadata_storage.by_name(name) unless raise IocRb::Errors::MissingBeanError, "Bean with name :#{name} is not defined" end () end |
#get_bean_with_metadata(bean_metadata) ⇒ Object
Get bean by the specified bean metadata
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ioc_rb/bean_factory.rb', line 37 def () case .scope when :singleton @singleton_scope.get_bean() when :prototype @prototype_scope.get_bean() when :request @request_scope.get_bean() else raise IocRb::Errors::UnsupportedScopeError, "Bean with name :#{.name} has unsupported scope :#{.scope}" end end |