Class: Moneta::Adapters::ActiveRecord::V5Backend Private

Inherits:
Object
  • Object
show all
Defined in:
lib/moneta/adapters/activerecord/v5_backend.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:, connection: nil, **options) ⇒ V5Backend

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of V5Backend.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 35

def initialize(table:, connection: nil, **options)
  @table_name = table
  @spec =
    case connection
    when Symbol
      connection
    when Hash, String
      # Normalize the connection specification to a hash
      resolver = ::ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new \
        'dummy' => connection

      # Turn the config into a standardised hash, sans a couple of bits
      hash = resolver.resolve(:dummy)
      hash.delete('name')
      hash.delete(:username) # For security
      hash.delete(:password) # For security
      # Make a name unique to this config
      name = 'moneta?' + URI.encode_www_form(hash.to_a.sort)
      # Add into configurations unless its already there (initially done without locking for
      # speed)
      unless self.class.configurations.key? name
        self.class.connection_lock.synchronize do
          self.class.configurations[name] = connection \
            unless self.class.configurations.key? name
        end
      end

      name.to_sym
    when nil
      nil
    else
      raise "Unexpected connection: #{connection}"
    end
end

Class Attribute Details

.connection_lockObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 11

def connection_lock
  @connection_lock
end

Instance Attribute Details

#table_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 33

def table_name
  @table_name
end

Class Method Details

.establish_connection(spec_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



18
19
20
21
22
23
24
25
26
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 18

def establish_connection(spec_name)
  connection_lock.synchronize do
    if connection_pool = retrieve_connection_pool(spec_name)
      connection_pool
    else
      connection_handler.establish_connection(spec_name.to_sym)
    end
  end
end

.retrieve_connection_pool(spec_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 14

def retrieve_connection_pool(spec_name)
  connection_handler.retrieve_connection_pool(spec_name.to_s)
end

.retrieve_or_establish_connection_pool(spec_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



28
29
30
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 28

def retrieve_or_establish_connection_pool(spec_name)
  retrieve_connection_pool(spec_name) || establish_connection(spec_name)
end

Instance Method Details

#connection_poolObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



70
71
72
73
74
75
76
# File 'lib/moneta/adapters/activerecord/v5_backend.rb', line 70

def connection_pool
  if @spec
    self.class.retrieve_or_establish_connection_pool(@spec)
  else
    ::ActiveRecord::Base.connection_pool
  end
end