Module: RailsEnvSwitcher

Defined in:
lib/rails-env-switcher.rb,
lib/rails-env-switcher/version.rb

Defined Under Namespace

Modules: Pry, Switcher

Constant Summary collapse

VERSION =
'0.2.3'

Class Method Summary collapse

Class Method Details

.add_handler(handler) ⇒ Object



34
35
36
# File 'lib/rails-env-switcher.rb', line 34

def self.add_handler(handler)
  self.handlers << handler
end

.setupObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails-env-switcher.rb', line 38

def self.setup
  # The order matters: handlers are executed top to bottom when switching env
  add_handler Switcher::Bundler
  add_handler Switcher::Rails
  add_handler Switcher::ActiveRecord     if defined?(::ActiveRecord::Base)
  add_handler Switcher::Mongoid          if defined?(::Mongoid)
  add_handler Switcher::CopycopterClient if defined?(::CopycopterClient::Rails)
  add_handler Switcher::Reloader

  RailsEnvSwitcher::Pry.setup if defined?(::Pry)
end

.switch_env(env, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails-env-switcher.rb', line 19

def self.switch_env(env, options={})
  old_env = Rails.env

  if old_env != env
    self.handlers.each do |handler|
      handler.switch_env(old_env, env, options)
    end
  else
    # Always reload if needed
    Switcher::Reloader.switch_env(env, env, options)
  end
end

.with_env(env, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rails-env-switcher.rb', line 6

def self.with_env(env, options={})
  options = options.dup
  old_env = Rails.env

  begin
    switch_env(env, options)
    yield
  ensure
    options.delete(:reload) # not reloading again once we switch back
    switch_env(old_env, options)
  end
end