Class: Callme::DepFactory
- Inherits:
-
Object
- Object
- Callme::DepFactory
- Defined in:
- lib/callme/dep_factory.rb
Overview
Instantiates deps according to their scopes
Instance Attribute Summary collapse
-
#const_loader ⇒ Object
readonly
Returns the value of attribute const_loader.
Instance Method Summary collapse
-
#create_dep_and_save(dep_metadata, deps_storage) ⇒ Object
Create new dep instance according to the specified
dep_metadata. -
#delete_dep(name) ⇒ Object
Delete dep from the container by it’s
name. -
#get_dep(name) ⇒ Object
Get dep from the container by it’s
name. -
#get_dep_with_metadata(dep_metadata) ⇒ Object
Get dep by the specified dep metadata.
-
#initialize(const_loader, deps_metadata_storage) ⇒ DepFactory
constructor
Constructor.
Constructor Details
#initialize(const_loader, deps_metadata_storage) ⇒ DepFactory
Constructor
7 8 9 10 11 12 13 |
# File 'lib/callme/dep_factory.rb', line 7 def initialize(const_loader, ) @const_loader = const_loader = @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.
3 4 5 |
# File 'lib/callme/dep_factory.rb', line 3 def const_loader @const_loader end |
Instance Method Details
#create_dep_and_save(dep_metadata, deps_storage) ⇒ Object
Create new dep instance according to the specified dep_metadata
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 |
# File 'lib/callme/dep_factory.rb', line 41 def create_dep_and_save(, deps_storage) if .dep_class.is_a?(Class) dep_class = .dep_class else dep_class = const_loader.load_const(.dep_class) .fetch_attrs!(dep_class) end dep = .instance ? dep_class.new : dep_class if .has_contract? contract_validator.validate(dep_class, .contract, const_loader) end if .has_factory_method? set_dep_dependencies(dep, ) dep = dep.send(.factory_method) deps_storage[.name] = dep else # put to container first to prevent circular dependencies deps_storage[.name] = dep set_dep_dependencies(dep, ) end dep end |
#delete_dep(name) ⇒ Object
Delete dep from the container by it’s name.
70 71 72 73 74 75 76 |
# File 'lib/callme/dep_factory.rb', line 70 def delete_dep(name) = .by_name(name) unless raise Callme::Errors::MissingDepError, "Dep with name :#{name} is not defined" end ().delete_dep() end |
#get_dep(name) ⇒ Object
Get dep from the container by it’s name. According to the dep scope it will be newly created or returned already instantiated dep
21 22 23 24 25 26 27 |
# File 'lib/callme/dep_factory.rb', line 21 def get_dep(name) = .by_name(name) unless raise Callme::Errors::MissingDepError, "Dep with name :#{name} is not defined" end () end |
#get_dep_with_metadata(dep_metadata) ⇒ Object
Get dep by the specified dep metadata
32 33 34 |
# File 'lib/callme/dep_factory.rb', line 32 def () ().get_dep() end |