Class: DependencyManager::Factory

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from ConfigSchemaMacros

included, #validate, #validate!

Constructor Details

#initialize(app_context: nil, factory_config:) ⇒ Factory

Creates a new Factory.

Parameters:

  • app_context: (defaults to: nil)

    nil [AppContext] Application context information. Defaulted to nil in case users do not need this information.

  • factory_config: (Hash[Symbol, Any])

    Configuration specific to the 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_nameObject



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

Parameters:

  • s (String, Symbol)

Returns:

  • (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

.dependenciesArray[Symbol]

Dependencies of the class under the factory that it needs to initialize.

Returns:

  • (Array[Symbol])


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_nameSymbol

Name of the expected dependency to be generated

Returns:

  • (Symbol)


101
102
103
# File 'lib/dependency_manager/factory.rb', line 101

def dependency_name
  name.to_s.sub(/_factory$/, '').to_sym
end

.factoriesArray[Symbol]

Get all available factory names except the Base factory

Returns:

  • (Array[Symbol])


59
60
61
# File 'lib/dependency_manager/factory.rb', line 59

def factories
  @factories || Set.new
end

.factory_dependenciesArray[Symbol]

Dependencies of the factory itself to make sure factories load in the correct order.

Returns:

  • (Array[Symbol])


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

Parameters:

  • factory_name (Symbol)

Returns:

  • (Symbol)

    Constant 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

Parameters:

  • subclass (Class)

    The subclass



51
52
53
54
# File 'lib/dependency_manager/factory.rb', line 51

def inherited(subclass)
  @factories ||= Set.new
  @factories.add subclass
end

.nameString

Name of the factory

Returns:

  • (String)


94
95
96
# File 'lib/dependency_manager/factory.rb', line 94

def name
  underscore const_name
end

.optional_dependenciesArray[Symbol]

Optional arguments that are not strictly required, but used for additional functionality.

Returns:

  • (Array[Symbol])


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

.parametersObject



105
106
107
# File 'lib/dependency_manager/factory.rb', line 105

def parameters
  instance_method(:initialize).parameters
end

.required_dependenciesArray[Symbol]

Dependencies required to build the factory

Returns:

  • (Array[Symbol])


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

Parameters:

  • const_name (Symbol)

Returns:

  • (Symbol)


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

#buildObject

Used to build the dependency

Raises:

  • (NotImplementedError)


175
176
177
# File 'lib/dependency_manager/factory.rb', line 175

def build
  raise NotImplementedError
end

#configurationObject

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.

Raises:

  • (Hash[Symbol, Any])


186
187
188
# File 'lib/dependency_manager/factory.rb', line 186

def configuration
  @configuration ||= deep_merge(default_configuration, @factory_config)
end

#default_configurationHash[Symbol, Any]

Default configuration of the Factory

Returns:

  • (Hash[Symbol, Any])


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 “‘

Returns:

  • (FalseClass)

    Disabled by default



216
217
218
# File 'lib/dependency_manager/factory.rb', line 216

def enabled?
  false
end

#load_requirementsObject

Used to load and require any associated external dependencies.

Raises:

  • (NotImplementedError)


200
201
202
# File 'lib/dependency_manager/factory.rb', line 200

def load_requirements
  raise NotImplementedError
end