Class: Spikard::Provide

Inherits:
Object
  • Object
show all
Defined in:
lib/spikard/provide.rb

Overview

Wrapper class for dependency providers

This class wraps factory functions and configuration for dependency injection. It provides a consistent API across Python, Node.js, and Ruby bindings.

Examples:

Factory with caching

app.provide("db", Spikard::Provide.new(method("create_db"), cacheable: true))

Factory with dependencies

app.provide("auth", Spikard::Provide.new(
  method("create_auth_service"),
  depends_on: ["db", "cache"],
  singleton: true
))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, depends_on: [], singleton: false, cacheable: true) ⇒ Provide

Create a new dependency provider

Parameters:

  • factory (Proc, Method)

    The factory function that creates the dependency value

  • depends_on (Array<String, Symbol>) (defaults to: [])

    List of dependency keys this factory depends on

  • singleton (Boolean) (defaults to: false)

    Whether to cache the value globally (default: false)

  • cacheable (Boolean) (defaults to: true)

    Whether to cache the value per-request (default: true)



27
28
29
30
31
32
# File 'lib/spikard/provide.rb', line 27

def initialize(factory, depends_on: [], singleton: false, cacheable: true)
  @factory = factory
  @depends_on = Array(depends_on).map(&:to_s)
  @singleton = singleton
  @cacheable = cacheable
end

Instance Attribute Details

#cacheableObject (readonly)

Returns the value of attribute cacheable.



19
20
21
# File 'lib/spikard/provide.rb', line 19

def cacheable
  @cacheable
end

#depends_onObject (readonly)

Returns the value of attribute depends_on.



19
20
21
# File 'lib/spikard/provide.rb', line 19

def depends_on
  @depends_on
end

#factoryObject (readonly)

Returns the value of attribute factory.



19
20
21
# File 'lib/spikard/provide.rb', line 19

def factory
  @factory
end

#singletonObject (readonly)

Returns the value of attribute singleton.



19
20
21
# File 'lib/spikard/provide.rb', line 19

def singleton
  @singleton
end

Instance Method Details

#async?Boolean

Check if the factory is async (based on method arity or other heuristics)

Returns:

  • (Boolean)

    True if the factory appears to be async



37
38
39
40
41
# File 'lib/spikard/provide.rb', line 37

def async?
  # Ruby doesn't have explicit async/await like Python/JS
  # We could check if it returns a Thread or uses Fiber
  false
end

#async_generator?Boolean

Check if the factory is an async generator

Returns:

  • (Boolean)

    True if the factory is an async generator



46
47
48
# File 'lib/spikard/provide.rb', line 46

def async_generator?
  false
end