Class: Sequelizer::ConnectionMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/sequelizer/connection_maker.rb

Overview

ConnectionMaker

Class that handles loading/interpreting the database options and creates the Sequel connection. This class is responsible for:

  • Loading configuration from multiple sources

  • Creating standard Sequel database connections

Examples:

Basic usage

maker = ConnectionMaker.new(adapter: 'postgres', host: 'localhost')
db = maker.connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ ConnectionMaker

Creates a new ConnectionMaker instance.

If no options are provided, attempts to read options from multiple sources in order of precedence:

  1. .env file

  2. Environment variables

  3. config/database.yml

  4. ~/.config/sequelizer/database.yml

Parameters:

  • options (Hash, nil) (defaults to: nil)

    database connection options

Options Hash (options):

  • :adapter (String)

    database adapter (e.g., ‘postgres’, ‘mysql2’)

  • :host (String)

    database host

  • :port (Integer)

    database port

  • :database (String)

    database name

  • :username (String)

    database username

  • :password (String)

    database password

  • :search_path (String)

    PostgreSQL schema search path



39
40
41
# File 'lib/sequelizer/connection_maker.rb', line 39

def initialize(options = nil)
  @options = Options.new(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/sequelizer/connection_maker.rb', line 20

def options
  @options
end

Instance Method Details

#connectionSequel::Database

Returns a Sequel connection to the database.

This method creates a standard Sequel database connection using the configured options.

Examples:

connection = maker.connection
users = connection[:users].all

Returns:

  • (Sequel::Database)

    configured database connection

Raises:

  • (Sequel::Error)

    if connection fails



54
55
56
57
58
59
60
61
# File 'lib/sequelizer/connection_maker.rb', line 54

def connection
  opts = options.to_hash
  extensions = options.extensions

  conn = create_sequel_connection(opts)
  conn.extension(*extensions)
  conn
end