Module: DbCharmer::ActiveRecordExtensions::ClassMethods

Defined in:
lib/db_charmer/active_record_extensions.rb

Constant Summary collapse

@@db_charmer_opts =

{}
@@db_charmer_connection_proxies =

{}
@@db_charmer_slaves =

{}
@@db_charmer_connection_levels =

Hash.new(0)

Instance Method Summary collapse

Instance Method Details

#db_charmer_connection_levelObject



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

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

#db_charmer_connection_level=(level) ⇒ Object



50
51
52
# File 'lib/db_charmer/active_record_extensions.rb', line 50

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

#db_charmer_connection_proxyObject



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

def db_charmer_connection_proxy
  @@db_charmer_connection_proxies[self.name]
end

#db_charmer_connection_proxy=(proxy) ⇒ Object



25
26
27
# File 'lib/db_charmer/active_record_extensions.rb', line 25

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

#db_charmer_optsObject



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

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

#db_charmer_opts=(opts) ⇒ Object



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

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

#db_charmer_random_slaveObject



43
44
45
46
# File 'lib/db_charmer/active_record_extensions.rb', line 43

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

#db_charmer_slavesObject



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

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

#db_charmer_slaves=(slaves) ⇒ Object



35
36
37
# File 'lib/db_charmer/active_record_extensions.rb', line 35

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

#db_charmer_top_level_connection?Boolean

Returns:

  • (Boolean)


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

def db_charmer_top_level_connection?
  db_charmer_connection_level.zero?
end

#establish_real_connection_if_exists(name, should_exist = false) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/db_charmer/active_record_extensions.rb', line 5

def establish_real_connection_if_exists(name, should_exist = false)
  config = configurations[RAILS_ENV][name.to_s]
  if should_exist && !config
    raise ArgumentError, "Invalid connection name (does not exist in database.yml): #{RAILS_ENV}/#{name}"
  end
  establish_connection(config) if config
end

#hijack_connection!Object




63
64
65
66
67
68
69
70
71
# File 'lib/db_charmer/active_record_extensions.rb', line 63

def hijack_connection!
  return if self.respond_to?(:connection_with_magic)
  class << self
    def connection_with_magic
      db_charmer_connection_proxy || connection_without_magic
    end
    alias_method_chain :connection, :magic
  end
end