Class: Familia::Connection::StandaloneConnectionHandler

Inherits:
BaseConnectionHandler show all
Defined in:
lib/familia/connection/handlers.rb

Overview

Handler for standalone DataType objects without a parent

Provides connection resolution for DataType objects that are created independently rather than being attached to a Horreum model. Checks for instance-level @dbclient first, then falls back to creating a connection based on logical_database option or global Familia connection.

This enables standalone DataType usage patterns like Rack::Session implementations where DataType objects need independent connection management and transaction support.

Examples:

Standalone DataType with custom connection

leaderboard = Familia::SortedSet.new('game:leaderboard')
leaderboard.dbclient = ConnectionPool.new { Redis.new }

Standalone DataType with logical_database option

cache = Familia::HashKey.new('app:cache', logical_database: 2)

Instance Method Summary collapse

Constructor Details

#initialize(data_type) ⇒ StandaloneConnectionHandler

Returns a new instance of StandaloneConnectionHandler.



288
289
290
# File 'lib/familia/connection/handlers.rb', line 288

def initialize(data_type)
  @data_type = data_type
end

Instance Method Details

#handle(uri) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/familia/connection/handlers.rb', line 292

def handle(uri)
  # If a specific URI is provided, always use it to get a connection.
  if uri
    connection = Familia.dbclient(uri)
    Familia.trace :DBCLIENT_STANDALONE_DATATYPE, @data_type.dbkey,
                 "Created standalone connection for specific URI: #{uri}"
    return connection
  end

  # Use instance @dbclient if explicitly set and no URI was passed
  instance_dbclient = @data_type.instance_variable_get(:@dbclient)
  if instance_dbclient
    Familia.trace :DBCLIENT_DATATYPE_INSTANCE, @data_type.dbkey,
                 'Using DataType instance @dbclient'
    return instance_dbclient
  end

  # Fall back to creating connection based on opts or global
  target_uri = @data_type.opts[:logical_database]
  connection = Familia.dbclient(target_uri)

  Familia.trace :DBCLIENT_STANDALONE_DATATYPE, @data_type.dbkey,
               "Created standalone connection for #{target_uri || 'default'}"

  connection
end