Class: MemoriClient::ClientFactory
- Inherits:
-
Object
- Object
- MemoriClient::ClientFactory
- Includes:
- Singleton
- Defined in:
- lib/memori_client/client_factory.rb
Overview
ClientFactory is a singleton class responsible for creating and caching MemoriClient::Client instances for different namespaces and kinds.
This factory pattern ensures that only one client instance exists per namespace/kind combination, improving performance and resource usage.
Defined Under Namespace
Modules: Kinds
Instance Method Summary collapse
-
#get_client(namespace:, kind:, uri:, api_root:) ⇒ MemoriClient::Client
Gets or creates a cached client instance for the specified parameters.
-
#initialize ⇒ ClientFactory
constructor
Initializes a new ClientFactory instance.
Constructor Details
#initialize ⇒ ClientFactory
Initializes a new ClientFactory instance. Sets up empty hashes to cache backend and engine clients by namespace.
35 36 37 38 |
# File 'lib/memori_client/client_factory.rb', line 35 def initialize @backend_clients = {} @engine_clients = {} end |
Instance Method Details
#get_client(namespace:, kind:, uri:, api_root:) ⇒ MemoriClient::Client
Gets or creates a cached client instance for the specified parameters.
This method implements a lazy initialization pattern where clients are only created when first requested and then cached for subsequent use.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/memori_client/client_factory.rb', line 71 def get_client(namespace:, kind:, uri:, api_root:) # Validate that the provided kind is supported raise "Invalid kind: #{kind}" unless known_kinds.include?(kind) case kind when Kinds::BACKEND # Use memoization to return cached client or create new one @backend_clients[namespace] ||= MemoriClient::Client.new(namespace: namespace, kind: kind, uri: uri, api_root: api_root) when Kinds::ENGINE # Use memoization to return cached client or create new one @engine_clients[namespace] ||= MemoriClient::Client.new(namespace: namespace, kind: kind, uri: uri, api_root: api_root) end end |