Module: DbCharmer::ActiveRecord::ClassAttributes

Defined in:
lib/db_charmer/active_record/class_attributes.rb

Constant Summary collapse

@@db_charmer_opts =
{}
@@db_charmer_connection_proxies =

{}
@@db_charmer_default_connections =

{}
@@db_charmer_slaves =

{}
@@db_charmer_force_slave_reads =

{}
@@db_charmer_connection_levels =

Hash.new(0)
@@db_charmer_database_remappings =

Hash.new
@@db_charmer_allocated_shard_connection =

nil

Instance Method Summary collapse

Instance Method Details

#db_charmer_allocated_shard_connectionObject



106
107
108
# File 'lib/db_charmer/active_record/class_attributes.rb', line 106

def db_charmer_allocated_shard_connection
  @@db_charmer_allocated_shard_connection
end

#db_charmer_allocated_shard_connection=(conn) ⇒ Object



110
111
112
# File 'lib/db_charmer/active_record/class_attributes.rb', line 110

def db_charmer_allocated_shard_connection=(conn)
  @@db_charmer_allocated_shard_connection = conn
end

#db_charmer_connection_levelObject



75
76
77
# File 'lib/db_charmer/active_record/class_attributes.rb', line 75

def db_charmer_connection_level
  @@db_charmer_connection_levels[self.name] || 0
end

#db_charmer_connection_level=(level) ⇒ Object



71
72
73
# File 'lib/db_charmer/active_record/class_attributes.rb', line 71

def db_charmer_connection_level=(level)
  @@db_charmer_connection_levels[self.name] = level
end

#db_charmer_connection_proxyObject



19
20
21
# File 'lib/db_charmer/active_record/class_attributes.rb', line 19

def db_charmer_connection_proxy
  @@db_charmer_connection_proxies[self.name]
end

#db_charmer_connection_proxy=(proxy) ⇒ Object



15
16
17
# File 'lib/db_charmer/active_record/class_attributes.rb', line 15

def db_charmer_connection_proxy=(proxy)
  @@db_charmer_connection_proxies[self.name] = proxy
end

#db_charmer_database_remappingsObject



95
96
97
# File 'lib/db_charmer/active_record/class_attributes.rb', line 95

def db_charmer_database_remappings
  @@db_charmer_database_remappings
end

#db_charmer_database_remappings=(mappings) ⇒ Object



99
100
101
102
# File 'lib/db_charmer/active_record/class_attributes.rb', line 99

def db_charmer_database_remappings=(mappings)
  raise "Mappings must be nil or respond to []" if mappings && (! mappings.respond_to?(:[]))
  @@db_charmer_database_remappings = mappings || { }
end

#db_charmer_default_connectionObject



33
34
35
# File 'lib/db_charmer/active_record/class_attributes.rb', line 33

def db_charmer_default_connection
  @@db_charmer_default_connections[self.name]
end

#db_charmer_default_connection=(conn) ⇒ Object



29
30
31
# File 'lib/db_charmer/active_record/class_attributes.rb', line 29

def db_charmer_default_connection=(conn)
  @@db_charmer_default_connections[self.name] = conn
end

#db_charmer_force_slave_readsObject



58
59
60
# File 'lib/db_charmer/active_record/class_attributes.rb', line 58

def db_charmer_force_slave_reads
  @@db_charmer_force_slave_reads[self.name]
end

#db_charmer_force_slave_reads=(force) ⇒ Object



54
55
56
# File 'lib/db_charmer/active_record/class_attributes.rb', line 54

def db_charmer_force_slave_reads=(force)
  @@db_charmer_force_slave_reads[self.name] = force
end

#db_charmer_force_slave_reads?Boolean

Slave reads are used in two cases:

- per-model slave reads are enabled (see db_magic method for more details)
- global slave reads enforcing is enabled (in a controller action)

Returns:

  • (Boolean)


65
66
67
# File 'lib/db_charmer/active_record/class_attributes.rb', line 65

def db_charmer_force_slave_reads?
  db_charmer_force_slave_reads || DbCharmer.force_slave_reads?
end

#db_charmer_optsObject



9
10
11
# File 'lib/db_charmer/active_record/class_attributes.rb', line 9

def db_charmer_opts
  @@db_charmer_opts[self.name] || {}
end

#db_charmer_opts=(opts) ⇒ Object



5
6
7
# File 'lib/db_charmer/active_record/class_attributes.rb', line 5

def db_charmer_opts=(opts)
  @@db_charmer_opts[self.name] = opts
end

#db_charmer_random_slaveObject



47
48
49
50
# File 'lib/db_charmer/active_record/class_attributes.rb', line 47

def db_charmer_random_slave
  return nil unless db_charmer_slaves.any?
  db_charmer_slaves[rand(db_charmer_slaves.size)]
end

#db_charmer_remapped_connectionObject



85
86
87
88
89
90
91
92
93
# File 'lib/db_charmer/active_record/class_attributes.rb', line 85

def db_charmer_remapped_connection
  return nil if (db_charmer_connection_level || 0) > 0
  name = :master
  proxy = db_charmer_connection_proxy
  name = proxy.db_charmer_connection_name.to_sym if proxy

  remapped = @@db_charmer_database_remappings[name]
  remapped ? DbCharmer::ConnectionFactory.connect(remapped, true) : nil
end

#db_charmer_slavesObject



43
44
45
# File 'lib/db_charmer/active_record/class_attributes.rb', line 43

def db_charmer_slaves
  @@db_charmer_slaves[self.name] || []
end

#db_charmer_slaves=(slaves) ⇒ Object



39
40
41
# File 'lib/db_charmer/active_record/class_attributes.rb', line 39

def db_charmer_slaves=(slaves)
  @@db_charmer_slaves[self.name] = slaves
end

#db_charmer_top_level_connection?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/db_charmer/active_record/class_attributes.rb', line 79

def db_charmer_top_level_connection?
  db_charmer_connection_level.zero?
end

#reset_connection_proxiesObject



23
24
25
# File 'lib/db_charmer/active_record/class_attributes.rb', line 23

def reset_connection_proxies
  @@db_charmer_connection_proxies = {}
end

#table_name_without_schemaObject

For sharded models, return the table name without the schema prefix



115
116
117
118
119
120
121
# File 'lib/db_charmer/active_record/class_attributes.rb', line 115

def table_name_without_schema
  if self.respond_to?(:orig_table_name)
    self.orig_table_name
  else
    self.table_name
  end
end