Class: Callme::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. -
#delete_bean(name) ⇒ Object
Delete bean from the container by it’s
name. -
#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/callme/bean_factory.rb', line 12 def initialize(const_loader, ) @const_loader = const_loader @beans_metadata_storage = @singleton_scope = Callme::Scopes::SingletonScope.new(self) @prototype_scope = Callme::Scopes::PrototypeScope.new(self) @request_scope = Callme::Scopes::RequestScope.new(self) end |
Instance Attribute Details
#const_loader ⇒ Object (readonly)
Returns the value of attribute const_loader.
8 9 10 |
# File 'lib/callme/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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/callme/bean_factory.rb', line 46 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 |
#delete_bean(name) ⇒ Object
Delete bean from the container by it’s name.
70 71 72 73 74 75 76 |
# File 'lib/callme/bean_factory.rb', line 70 def delete_bean(name) = @beans_metadata_storage.by_name(name) unless raise Callme::Errors::MissingBeanError, "Bean with name :#{name} is not defined" end ().delete_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/callme/bean_factory.rb', line 26 def get_bean(name) = @beans_metadata_storage.by_name(name) unless raise Callme::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 |
# File 'lib/callme/bean_factory.rb', line 37 def () ().get_bean() end |