Class: DataFabric::ConnectionProxy

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

Instance Method Summary collapse

Constructor Details

#initialize(model_class, options) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy.



65
66
67
68
69
70
71
72
73
# File 'lib/data_fabric/connection_proxy.rb', line 65

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



91
92
93
94
# File 'lib/data_fabric/connection_proxy.rb', line 91

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

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/data_fabric/connection_proxy.rb', line 109

def connected?
  current_pool.connected?
end

#connectionObject



113
114
115
# File 'lib/data_fabric/connection_proxy.rb', line 113

def connection
  current_pool.connection
end

#connection_nameObject



96
97
98
# File 'lib/data_fabric/connection_proxy.rb', line 96

def connection_name
  connection_name_builder.join('_')
end

#current_poolObject



117
118
119
120
121
122
123
124
# File 'lib/data_fabric/connection_proxy.rb', line 117

def current_pool
  name = connection_name
  self.class.shard_pools[name] ||= begin
    config = ActiveRecord::Base.configurations[name]
    raise ArgumentError, "Unknown database config: #{name}, have #{ActiveRecord::Base.configurations.inspect}" unless config
    ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec_for(config))
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method)
  super || connection.respond_to?(method)
end

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



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

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

#with_masterObject



100
101
102
103
104
105
106
107
# File 'lib/data_fabric/connection_proxy.rb', line 100

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