Class: DependencyManager::Factory
- Inherits:
-
Object
- Object
- DependencyManager::Factory
- Includes:
- ConfigSchemaMacros
- Defined in:
- lib/dependency_manager/factory.rb
Overview
Base for all other factories, providing interface hints and generic functionality
### Initialize for Dependency Specifications
Every keyword argument used in the initialize function for a Factory is used to resolve the dependencies of the class with the exception of CONTEXT_DEPENDENCIES.
:keyreq represents a required argument, while :key represents an optional one:
“‘ruby def initialize(logger:, optional_dependency: nil, **dependencies)
super(**dependencies)
@logger = logger
@optional_dependency = optional_dependency
end “‘
This value could be nil or any other sane default value for the dependency specified.
The Factory implements several helper methods on its singleton class like dependencies, optional_dependencies, and factory_dependencies to help with constructing dependency chains.
Constant Summary collapse
- CONTEXT_DEPENDENCIES =
Dependencies that are always present and injected at a top level rather than by other factories
%i(app_context factory_config)
- KEYWORD_ARGS =
Keyword param types
%i(keyreq key)
- OPTIONAL_ARG =
:key
Class Method Summary collapse
- .const_name ⇒ Object
-
.constantize(s) ⇒ Symbol
Utility to constantize an underscored string or symbol.
-
.dependencies ⇒ Array[Symbol]
Dependencies of the class under the factory that it needs to initialize.
-
.dependency_name ⇒ Symbol
Name of the expected dependency to be generated.
-
.factories ⇒ Array[Symbol]
Get all available factory names except the Base factory.
-
.factory_dependencies ⇒ Array[Symbol]
Dependencies of the factory itself to make sure factories load in the correct order.
-
.get(factory_name) ⇒ Symbol
Get a factory by its underscored name.
-
.inherited(subclass) ⇒ void
Captures classes inheriting from Factory for later use.
-
.name ⇒ String
Name of the factory.
-
.optional_dependencies ⇒ Array[Symbol]
Optional arguments that are not strictly required, but used for additional functionality.
- .parameters ⇒ Object
-
.required_dependencies ⇒ Array[Symbol]
Dependencies required to build the factory.
-
.underscore(const_name) ⇒ Symbol
Underscores a constant name.
Instance Method Summary collapse
-
#build ⇒ Object
Used to build the dependency.
-
#configuration ⇒ Object
Used to generate configuration for the dependency, not always necessary for shorter builds.
-
#default_configuration ⇒ Hash[Symbol, Any]
Default configuration of the Factory.
-
#enabled? ⇒ FalseClass
Whether or not the dependency should be enabled.
-
#initialize(app_context: nil, factory_config:) ⇒ Factory
constructor
Creates a new Factory.
-
#load_requirements ⇒ Object
Used to load and require any associated external dependencies.
Methods included from ConfigSchemaMacros
included, #validate, #validate!
Constructor Details
#initialize(app_context: nil, factory_config:) ⇒ Factory
Creates a new Factory.
167 168 169 170 |
# File 'lib/dependency_manager/factory.rb', line 167 def initialize(app_context: nil, factory_config:) @app_context = app_context @factory_config = factory_config end |
Class Method Details
.const_name ⇒ Object
87 88 89 |
# File 'lib/dependency_manager/factory.rb', line 87 def const_name to_s.split('::').last end |
.constantize(s) ⇒ Symbol
Utility to constantize an underscored string or symbol
83 84 85 |
# File 'lib/dependency_manager/factory.rb', line 83 def constantize(s) s.to_s.split('_').map(&:capitalize).join.to_sym end |
.dependencies ⇒ Array[Symbol]
Dependencies of the class under the factory that it needs to initialize.
112 113 114 115 116 117 118 |
# File 'lib/dependency_manager/factory.rb', line 112 def dependencies dependencies = parameters .select { |type, _name| KEYWORD_ARGS.include?(type) } .map(&:last) dependencies - CONTEXT_DEPENDENCIES end |
.dependency_name ⇒ Symbol
Name of the expected dependency to be generated
101 102 103 |
# File 'lib/dependency_manager/factory.rb', line 101 def dependency_name name.to_s.sub(/_factory$/, '').to_sym end |
.factories ⇒ Array[Symbol]
Get all available factory names except the Base factory
59 60 61 |
# File 'lib/dependency_manager/factory.rb', line 59 def factories @factories || Set.new end |
.factory_dependencies ⇒ Array[Symbol]
Dependencies of the factory itself to make sure factories load in the correct order.
124 125 126 |
# File 'lib/dependency_manager/factory.rb', line 124 def factory_dependencies dependencies.map { |d| "#{d}_factory".to_sym } end |
.get(factory_name) ⇒ Symbol
Get a factory by its underscored name
68 69 70 71 72 73 74 75 76 |
# File 'lib/dependency_manager/factory.rb', line 68 def get(factory_name) const_name = constantize(factory_name) unless const_defined?(const_name) raise ArgumentError, "Tried to get non-existant Factory. Did you remember to define it?: #{const_name}" end const_get const_name end |
.inherited(subclass) ⇒ void
This method returns an undefined value.
Captures classes inheriting from Factory for later use
51 52 53 54 |
# File 'lib/dependency_manager/factory.rb', line 51 def inherited(subclass) @factories ||= Set.new @factories.add subclass end |
.name ⇒ String
Name of the factory
94 95 96 |
# File 'lib/dependency_manager/factory.rb', line 94 def name underscore const_name end |
.optional_dependencies ⇒ Array[Symbol]
Optional arguments that are not strictly required, but used for additional functionality.
139 140 141 142 143 144 145 |
# File 'lib/dependency_manager/factory.rb', line 139 def optional_dependencies optionals = parameters .select { |type, _name| type == OPTIONAL_ARG } .map(&:last) optionals - CONTEXT_DEPENDENCIES end |
.parameters ⇒ Object
105 106 107 |
# File 'lib/dependency_manager/factory.rb', line 105 def parameters instance_method(:initialize).parameters end |
.required_dependencies ⇒ Array[Symbol]
Dependencies required to build the factory
131 132 133 |
# File 'lib/dependency_manager/factory.rb', line 131 def required_dependencies dependencies - optional_dependencies end |
.underscore(const_name) ⇒ Symbol
Underscores a constant name
152 153 154 |
# File 'lib/dependency_manager/factory.rb', line 152 def underscore(const_name) const_name.gsub(/([^\^])([A-Z])/,'\1_\2').downcase.to_sym end |
Instance Method Details
#build ⇒ Object
Used to build the dependency
175 176 177 |
# File 'lib/dependency_manager/factory.rb', line 175 def build raise NotImplementedError end |
#configuration ⇒ Object
Used to generate configuration for the dependency, not always necessary for shorter builds.
As a default will be an alias for ‘@factory_config`, and is used as a hook point for any validation.
186 187 188 |
# File 'lib/dependency_manager/factory.rb', line 186 def configuration @configuration ||= deep_merge(default_configuration, @factory_config) end |
#default_configuration ⇒ Hash[Symbol, Any]
Default configuration of the Factory
193 194 195 |
# File 'lib/dependency_manager/factory.rb', line 193 def default_configuration {} end |
#enabled? ⇒ FalseClass
Whether or not the dependency should be enabled. It is suggested to use this as a guard when building dependencies:
“‘ruby def build
return unless enabled?
# ...
end “‘
216 217 218 |
# File 'lib/dependency_manager/factory.rb', line 216 def enabled? false end |
#load_requirements ⇒ Object
Used to load and require any associated external dependencies.
200 201 202 |
# File 'lib/dependency_manager/factory.rb', line 200 def load_requirements raise NotImplementedError end |