Module: Kaal::Internal::ActiveRecord::ConnectionSupport

Defined in:
lib/kaal/internal/active_record/connection_support.rb

Overview

Establishes and reuses the Active Record connection for adapter models.

Constant Summary collapse

CONFIGURE_MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.configs_match?(current_config, target_config) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/kaal/internal/active_record/connection_support.rb', line 80

def configs_match?(current_config, target_config)
  return true if current_config == target_config

  current_url = current_config.is_a?(Hash) ? current_config[:url] : nil
  target_url = target_config.is_a?(Hash) ? target_config[:url] : nil
  !!(current_url && target_url && current_url == target_url)
end

.configure!(connection = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kaal/internal/active_record/connection_support.rb', line 16

def configure!(connection = nil)
  return BaseRecord unless connection

  CONFIGURE_MUTEX.synchronize do
    current_config = current_connection_config
    target_config = normalize_connection_config(connection)
    return BaseRecord if configs_match?(current_config, target_config) && connection_active?

    BaseRecord.establish_connection(connection)
  end
  BaseRecord
end

.connection_active?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/kaal/internal/active_record/connection_support.rb', line 88

def connection_active?
  BaseRecord.connection.active?
rescue ::ActiveRecord::ConnectionNotEstablished, StandardError
  false
end

.current_connection_configObject



39
40
41
42
43
44
# File 'lib/kaal/internal/active_record/connection_support.rb', line 39

def current_connection_config
  db_config = BaseRecord.connection_db_config
  normalize_connection_config(extract_connection_config(db_config))
rescue ::ActiveRecord::ConnectionNotEstablished
  nil
end

.extract_connection_config(connection) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kaal/internal/active_record/connection_support.rb', line 46

def extract_connection_config(connection)
  case connection
  when Hash
    connection
  when String
    { url: connection }
  else
    config = connection.configuration_hash
    url = begin
      connection.url
    rescue NoMethodError
      nil
    end
    url ? config.merge(url: url) : config
  end
rescue NoMethodError
  nil
end

.integer_like?(value) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/kaal/internal/active_record/connection_support.rb', line 76

def integer_like?(value)
  value.is_a?(Integer) || value.to_s.match?(/\A\d+\z/)
end

.normalize_connection_config(connection) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/kaal/internal/active_record/connection_support.rb', line 29

def normalize_connection_config(connection)
  config = extract_connection_config(connection)
  return connection unless config

  config.each_with_object({}) do |(key, value), normalized|
    normalized_key = key.to_sym
    normalized[normalized_key] = normalize_connection_value(normalized_key, value)
  end
end

.normalize_connection_value(key, value) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/kaal/internal/active_record/connection_support.rb', line 65

def normalize_connection_value(key, value)
  case key
  when :adapter
    value.to_s.downcase
  when :port
    integer_like?(value) ? value.to_i : value
  else
    value
  end
end