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.



46
47
48
49
50
51
52
53
54
# File 'lib/data_fabric/connection_proxy.rb', line 46

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



73
74
75
76
# File 'lib/data_fabric/connection_proxy.rb', line 73

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.



44
45
46
# File 'lib/data_fabric/connection_proxy.rb', line 44

def spec
  @spec
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


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

def connected?
  current_pool.connected?
end

#connectionObject



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

def connection
  current_pool.connection
end

#connection_nameObject



78
79
80
# File 'lib/data_fabric/connection_proxy.rb', line 78

def connection_name
  connection_name_builder.join('_')
end

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



62
63
64
65
66
67
68
69
70
71
# File 'lib/data_fabric/connection_proxy.rb', line 62

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



82
83
84
85
86
87
88
89
# File 'lib/data_fabric/connection_proxy.rb', line 82

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