Module: Spikard::ProvideSupport
- Included in:
- App
- Defined in:
- lib/spikard/provide.rb
Overview
Dependency Injection support for Spikard applications
Provides methods for registering and managing dependencies that can be automatically injected into route handlers.
Instance Method Summary collapse
-
#dependencies ⇒ Hash
private
Get all registered dependencies.
-
#provide(key, value = nil, depends_on: [], singleton: false, cacheable: true) {|**deps| ... } ⇒ self
Register a dependency in the DI container.
Instance Method Details
#dependencies ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get all registered dependencies
132 133 134 |
# File 'lib/spikard/provide.rb', line 132 def dependencies ensure_native_dependencies! end |
#provide(key, value = nil, depends_on: [], singleton: false, cacheable: true) {|**deps| ... } ⇒ self
Register a dependency in the DI container
This method supports three patterns:
-
**Value dependency**: Pass a value directly (e.g., string, number, object)
-
**Factory dependency**: Pass a block that computes the value
-
**Provide wrapper**: Pass a Spikard::Provide instance
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/spikard/provide.rb', line 110 def provide(key, value = nil, depends_on: [], singleton: false, cacheable: true, &block) key_str = key.to_s registry = ensure_native_dependencies! # Handle Provide wrapper instances if value.is_a?(Provide) registry.register_factory(key_str, value.factory, value.depends_on, value.singleton, value.cacheable) elsif block registry.register_factory(key_str, block, Array(depends_on).map(&:to_s), singleton, cacheable) else raise ArgumentError, 'Either provide a value or a block, not both' if value.nil? registry.register_value(key_str, value) end self end |