Class: DataFabric::ConnectionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/data_fabric/connection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, options) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy.



55
56
57
58
59
60
61
62
63
# File 'lib/data_fabric/connection_proxy.rb', line 55

def initialize(model_class, options)
  @model_class = model_class      
  @replicated  = options[:replicated]
  @shard_group = options[:shard_by]
  @prefix      = options[:prefix]
  set_role('slave') if @replicated

  @model_class.send :include, ActiveRecordConnectionMethods if @replicated
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



82
83
84
85
# File 'lib/data_fabric/connection_proxy.rb', line 82

def method_missing(method, *args, &block)
  DataFabric.logger.debug { "Calling #{method} on #{connection}" }
  connection.send(method, *args, &block)
end

Instance Attribute Details

#specObject

Returns the value of attribute spec.



53
54
55
# File 'lib/data_fabric/connection_proxy.rb', line 53

def spec
  @spec
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/data_fabric/connection_proxy.rb', line 100

def connected?
  current_pool.connected?
end

#connectionObject



104
105
106
# File 'lib/data_fabric/connection_proxy.rb', line 104

def connection
  current_pool.connection
end

#connection_nameObject



87
88
89
# File 'lib/data_fabric/connection_proxy.rb', line 87

def connection_name
  connection_name_builder.join('_')
end

#transaction(start_db_transaction = true, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/data_fabric/connection_proxy.rb', line 71

def transaction(start_db_transaction = true, &block)
  # Transaction is not re-entrant in SQLite 3 so we
  # need to track if we've already started an XA to avoid
  # calling it twice.
  return yield if in_transaction?

  with_master do
    connection.transaction(start_db_transaction, &block) 
  end
end

#with_masterObject



91
92
93
94
95
96
97
98
# File 'lib/data_fabric/connection_proxy.rb', line 91

def with_master
  # Allow nesting of with_master.
  old_role = current_role
  set_role('master')
  yield
ensure
  set_role(old_role)
end