Class: FreshConnection::SlaveConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/fresh_connection/slave_connection.rb

Constant Summary collapse

COUNT =
:fresh_connection_access_count
TARGET =
:fresh_connection_access_target

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ignore_configure_connection=(value) ⇒ Object (writeonly)

Sets the attribute ignore_configure_connection

Parameters:

  • value

    the value to set the attribute ignore_configure_connection to.



7
8
9
# File 'lib/fresh_connection/slave_connection.rb', line 7

def ignore_configure_connection=(value)
  @ignore_configure_connection = value
end

.ignore_models=(value) ⇒ Object (writeonly)

Sets the attribute ignore_models

Parameters:

  • value

    the value to set the attribute ignore_models to.



7
8
9
# File 'lib/fresh_connection/slave_connection.rb', line 7

def ignore_models=(value)
  @ignore_models = value
end

Class Method Details

.connection_manager=(manager) ⇒ Object



56
57
58
# File 'lib/fresh_connection/slave_connection.rb', line 56

def connection_manager=(manager)
  @connection_manager_class = manager
end

.ignore_configure_connection?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/fresh_connection/slave_connection.rb', line 52

def ignore_configure_connection?
  !!@ignore_configure_connection
end

.ignore_model?(model_klass) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fresh_connection/slave_connection.rb', line 39

def ignore_model?(model_klass)
  (@ignore_models || []).one? do |ignore_model|
    case ignore_model
    when String
      ignore_model == model_klass.name
    when ActiveRecord::Base
      model_klass.ancestors.include?(ignore_model)
    else
      false
    end
  end
end

.manage_access(model_klass, go_slave, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fresh_connection/slave_connection.rb', line 21

def manage_access(model_klass, go_slave, &block)
  if ignore_model?(model_klass)
    force_master_access(&block)
  else
    target = go_slave ? :slave : :master
    begin
      access_in(target)
      block.call
    ensure
      access_out
    end
  end
end

.put_aside!Object



17
18
19
# File 'lib/fresh_connection/slave_connection.rb', line 17

def put_aside!
  connection_manager.put_aside!
end

.raw_connectionObject



9
10
11
# File 'lib/fresh_connection/slave_connection.rb', line 9

def raw_connection
  slave_connection.raw_connection
end

.slave_access?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/fresh_connection/slave_connection.rb', line 35

def slave_access?
  Thread.current[TARGET] == :slave
end

.slave_connectionObject



13
14
15
# File 'lib/fresh_connection/slave_connection.rb', line 13

def slave_connection
  connection_manager.slave_connection
end