Module: DbCharmer::ActiveRecord::DbMagic

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

Instance Method Summary collapse

Instance Method Details

#db_magic(opt = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/db_charmer/active_record/db_magic.rb', line 5

def db_magic(opt = {})
  # Make sure we could use our connections management here
  hijack_connection!

  # Should requested connections exist in the config?
  should_exist = opt.has_key?(:should_exist) ? opt[:should_exist] : DbCharmer.connections_should_exist?

  # Main connection management
  setup_connection_magic(opt[:connection], should_exist)

  # Set up slaves pool
  opt[:slaves] ||= []
  opt[:slaves] = [ opt[:slaves] ].flatten
  opt[:slaves] << opt[:slave] if opt[:slave]

  # Forced reads are enabled for all models by default, could be disabled by the user
  forced_slave_reads = opt.has_key?(:force_slave_reads) ? opt[:force_slave_reads] : true

  # Setup all the slaves related magic if needed
  setup_slaves_magic(opt[:slaves], forced_slave_reads, should_exist)

  # Setup inheritance magic
  setup_children_magic(opt)

  # Setup sharding if needed
  if opt[:sharded]
    raise ArgumentError, "Can't use sharding on a model with slaves!" if opt[:slaves].any?
    setup_sharding_magic(opt[:sharded])
  end
end