Module: Shackles

Defined in:
lib/shackles.rb,
lib/shackles/railtie.rb,
lib/shackles/version.rb,
lib/shackles/helper_methods.rb,
lib/shackles/connection_handler.rb,
lib/shackles/connection_specification.rb

Defined Under Namespace

Modules: ConnectionHandler, ConnectionSpecification, HelperMethods Classes: Railtie

Constant Summary collapse

VERSION =
"1.4.2"

Class Method Summary collapse

Class Method Details

.activate(environment) ⇒ Object

switch environment for the duration of the block will keep the old connections around



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/shackles.rb', line 58

def activate(environment)
  environment ||= :master
  return yield if environment == self.environment
  begin
    old_environment = activate!(environment)
    activated_environments << environment
    yield
  ensure
    Thread.current[:shackles_environment] = old_environment
    ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
  end
end

.activate!(environment) ⇒ Object

for use from script/console ONLY



72
73
74
75
76
77
78
79
# File 'lib/shackles.rb', line 72

def activate!(environment)
  environment ||= :master
  save_handler
  old_environment = self.environment
  Thread.current[:shackles_environment] = environment
  ActiveRecord::Base.connection_handler = ensure_handler unless Rails.env.test?
  old_environment
end

.activated_environmentsObject



13
14
15
# File 'lib/shackles.rb', line 13

def activated_environments
  @activated_environments ||= Set.new()
end

.apply_config!(hash) ⇒ Object

for altering other pieces of config (i.e. username) will force a disconnect



41
42
43
44
# File 'lib/shackles.rb', line 41

def apply_config!(hash)
  global_config.merge!(hash)
  bump_sequence
end

.bump_sequenceObject



33
34
35
36
37
# File 'lib/shackles.rb', line 33

def bump_sequence
  @global_config_sequence ||= 1
  @global_config_sequence += 1
  ActiveRecord::Base::connection_handler.clear_all_connections!
end

.connection_handlersObject



51
52
53
54
# File 'lib/shackles.rb', line 51

def connection_handlers
  save_handler
  @connection_handlers
end

.environmentObject



5
6
7
# File 'lib/shackles.rb', line 5

def environment
  Thread.current[:shackles_environment] ||= :master
end

.global_configObject



9
10
11
# File 'lib/shackles.rb', line 9

def global_config
  @global_config ||= {}
end

.global_config_sequenceObject



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

def global_config_sequence
  @global_config_sequence ||= 1
end

.initialize!Object

semi-private



18
19
20
21
22
23
24
25
26
27
# File 'lib/shackles.rb', line 18

def initialize!
  require 'shackles/connection_handler'
  require 'shackles/connection_specification'
  require 'shackles/helper_methods'

  activated_environments << Shackles.environment

  ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ConnectionHandler)
  ActiveRecord::ConnectionAdapters::ConnectionSpecification.prepend(ConnectionSpecification)
end

.remove_config!(key) ⇒ Object



46
47
48
49
# File 'lib/shackles.rb', line 46

def remove_config!(key)
  global_config.delete(key)
  bump_sequence
end