Class: Mortymer::Container
- Inherits:
-
Object
- Object
- Mortymer::Container
- Defined in:
- lib/mortymer/container.rb
Overview
Base container for dependency injection
Defined Under Namespace
Classes: DependencyError, Error, NotFoundError
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Class Method Summary collapse
-
.duplicate ⇒ Object
Create a new container with a copy of the current registry.
-
.instance ⇒ Object
Get the global container instance.
-
.method_missing(method_name, *args, &block) ⇒ Object
Delegate instance methods to the singleton instance.
-
.registry ⇒ Object
Initialize the container storage.
- .respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Instance Method Summary collapse
- #duplicate ⇒ Object
-
#initialize(initial_registry = Concurrent::Hash.new) ⇒ Container
constructor
A new instance of Container.
-
#register_constant(constant, implementation = nil, &block) ⇒ Object
Register a constant with its implementation.
-
#resolve_constant(constant, resolution_stack = []) ⇒ Object
Resolve a constant to its implementation.
Constructor Details
#initialize(initial_registry = Concurrent::Hash.new) ⇒ Container
Returns a new instance of Container.
44 45 46 |
# File 'lib/mortymer/container.rb', line 44 def initialize(initial_registry = Concurrent::Hash.new) @registry = initial_registry end |
Instance Attribute Details
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
42 43 44 |
# File 'lib/mortymer/container.rb', line 42 def registry @registry end |
Class Method Details
.duplicate ⇒ Object
Create a new container with a copy of the current registry
24 25 26 |
# File 'lib/mortymer/container.rb', line 24 def duplicate new(registry.dup) end |
.instance ⇒ Object
Get the global container instance
14 15 16 |
# File 'lib/mortymer/container.rb', line 14 def instance @instance ||= new end |
.method_missing(method_name, *args, &block) ⇒ Object
Delegate instance methods to the singleton instance
29 30 31 32 33 34 35 |
# File 'lib/mortymer/container.rb', line 29 def method_missing(method_name, *args, &block) if instance.respond_to?(method_name) instance.public_send(method_name, *args, &block) else super end end |
.registry ⇒ Object
Initialize the container storage
19 20 21 |
# File 'lib/mortymer/container.rb', line 19 def registry instance.registry end |
.respond_to_missing?(method_name, include_private = false) ⇒ Boolean
37 38 39 |
# File 'lib/mortymer/container.rb', line 37 def respond_to_missing?(method_name, include_private = false) instance.respond_to?(method_name, include_private) || super end |
Instance Method Details
#duplicate ⇒ Object
48 49 50 |
# File 'lib/mortymer/container.rb', line 48 def duplicate self.class.new(registry.dup) end |
#register_constant(constant, implementation = nil, &block) ⇒ Object
Register a constant with its implementation
55 56 57 58 |
# File 'lib/mortymer/container.rb', line 55 def register_constant(constant, implementation = nil, &block) key = constant_to_key(constant) registry[key] = block || implementation end |
#resolve_constant(constant, resolution_stack = []) ⇒ Object
Resolve a constant to its implementation
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mortymer/container.rb', line 64 def resolve_constant(constant, resolution_stack = []) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity key = constant_to_key(constant) # Check for circular dependencies if resolution_stack.include?(key) raise DependencyError, "Circular dependency detected: #{resolution_stack.join(" -> ")} -> #{key}" end # Return cached instance if available return registry[key] if registry[key] && !registry[key].is_a?(Class) && !registry[key].is_a?(Proc) implementation = registry[key] # If not registered, try to resolve the constant from string/symbol if !implementation && (constant.is_a?(String) || constant.is_a?(Symbol)) begin const_name = constant.to_s implementation = Object.const_get(const_name) registry[key] = implementation rescue NameError raise NotFoundError, "No implementation found for #{key}" end end # If not registered, try to auto-resolve the constant if it's a class if !implementation && constant.is_a?(Class) implementation = constant registry[key] = implementation end raise NotFoundError, "No implementation found for #{key}" unless implementation # Add current constant to resolution stack resolution_stack.push(key) result = resolve_implementation(implementation, key, resolution_stack) resolution_stack.pop result end |