rails-env-switcher 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ Rails Env Switcher
2
+ ===================
3
+
4
+ Rails Env Switcher allows your to switch from a rails environmemnt to another one.
5
+ Best served chilled with [irb-config](https://github.com/nviennot/irb-config).
6
+
7
+ If you have Pry installed, it will add a "env" command. Example:
8
+
9
+ ```
10
+ pafy@bisou ~/prj/rails-prj [master●] % rails c
11
+ Loading development environment (Rails 3.2.8)
12
+ ~/prj/crowdtap/rails-prj (development) > env test
13
+ ~/prj/crowdtap/rails-prj (test) > Rails.env
14
+ => "test"
15
+ ~/prj/crowdtap/rails-prj (test) > exit
16
+ ~/prj/crowdtap/rails-prj (development) > Rails.env
17
+ => "development"
18
+
19
+ ```
20
+
21
+
22
+ Why is it useful?
23
+ -----------------
24
+
25
+ We want to run tests from the rails console.
26
+
27
+ ### Watch the screencast
28
+
29
+ [![Watch the screencast!](https://s3.amazonaws.com/velvetpulse/screencasts/irb-config-screencast.jpg)](http://velvetpulse.com/2012/11/19/improve-your-ruby-workflow-by-integrating-vim-tmux-pry/)
30
+
31
+
32
+ TODO
33
+ ----
34
+
35
+ * Middleware pattern
36
+ * Testing
37
+
38
+ License
39
+ -------
40
+
41
+ MIT License
@@ -0,0 +1,47 @@
1
+ module RailsEnvSwitcher
2
+ autoload :Pry, 'rails-env-switcher/pry'
3
+ autoload :Switcher, 'rails-env-switcher/switcher'
4
+ autoload :ConsoleMethods, 'rails-env-switcher/console_methods'
5
+
6
+ def self.with_env(env, options={})
7
+ options = options.dup
8
+ old_env = Rails.env
9
+
10
+ begin
11
+ switch_env(env, options)
12
+ yield
13
+ ensure
14
+ switch_env(old_env, options)
15
+ end
16
+ end
17
+
18
+ def self.switch_env(env, options={})
19
+ old_env = Rails.env
20
+
21
+ if old_env != env
22
+ self.handlers.each do |handler|
23
+ handler.switch_env(old_env, env, options)
24
+ end
25
+ end
26
+ end
27
+
28
+ mattr_accessor :handlers
29
+ self.handlers = []
30
+ def self.add_handler(handler)
31
+ self.handlers << handler
32
+ end
33
+
34
+ def self.setup
35
+ # The order matters: handlers are executed top to bottom when switching env
36
+ add_handler Switcher::Bundler
37
+ add_handler Switcher::Rails
38
+ add_handler Switcher::ActiveRecord if defined?(::ActiveRecord::Base)
39
+ add_handler Switcher::Mongoid if defined?(::Mongoid)
40
+ add_handler Switcher::CopycopterClient if defined?(::CopycopterClient::Rails)
41
+ add_handler Switcher::Reloader
42
+
43
+ RailsEnvSwitcher::Pry.setup if defined?(::Pry)
44
+ end
45
+
46
+ setup
47
+ end
@@ -0,0 +1,23 @@
1
+ module RailsEnvSwitcher::Pry
2
+ def self.setup
3
+ ::Pry::CommandSet.new do
4
+ create_command "env", "Switch environment. ctrl+d to leave" do
5
+ group "Environment"
6
+ def process(env)
7
+ unless env
8
+ puts "Usage: 'env test' for example"
9
+ end
10
+
11
+ RailsEnvSwitcher.with_env(env) do
12
+ # irb-config compatibility
13
+ if defined?(::IRB::Pry::TopLevel)
14
+ TopLevel.new.pry
15
+ else
16
+ env.pry
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end.tap { |cmd| ::Pry::Commands.import cmd }
22
+ end
23
+ end
@@ -0,0 +1,8 @@
1
+ module RailsEnvSwitcher::Switcher
2
+ autoload :ActiveRecord, 'rails-env-switcher/switcher/active_record'
3
+ autoload :Bundler, 'rails-env-switcher/switcher/bundler'
4
+ autoload :CopycopterClient, 'rails-env-switcher/switcher/copycopter_client'
5
+ autoload :Mongoid, 'rails-env-switcher/switcher/mongoid'
6
+ autoload :Rails, 'rails-env-switcher/switcher/rails'
7
+ autoload :Reloader, 'rails-env-switcher/switcher/reloader'
8
+ end
@@ -0,0 +1,9 @@
1
+ module RailsEnvSwitcher::Switcher::ActiveRecord
2
+ def self.switch_env(old_env, env, options={})
3
+ if ::ActiveRecord::Base.connected?
4
+ ::ActiveRecord::Base.clear_cache! if ::ActiveRecord::Base.respond_to? :clear_cache
5
+ ::ActiveRecord::Base.clear_all_connections!
6
+ ::ActiveRecord::Base.establish_connection
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module RailsEnvSwitcher::Switcher::Bundler
2
+ def self.switch_env(old_env, env, options={})
3
+ Bundler.require(env)
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module RailsEnvSwitcher::Switcher::CopycopterClient
2
+ def self.switch_env(old_env, env, options={})
3
+ ::CopycopterClient::Rails.initialize
4
+ end
5
+
6
+ def self.setup
7
+ conf = ::CopycopterClient.configuration
8
+ def conf.environment_name
9
+ ::Rails.env
10
+ end
11
+
12
+ ::CopycopterClient::RequestSync.class_eval do
13
+ def call(env)
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+
19
+ setup if defined?(::CopycopterClient) && defined?(::Rails)
20
+ end
@@ -0,0 +1,10 @@
1
+ module RailsEnvSwitcher::Switcher::Mongoid
2
+ def self.switch_env(old_env, env, options={})
3
+ old_level = Mongoid.logger.level
4
+ Mongoid.logger.level = Logger::WARN
5
+ Mongoid.load!("./config/mongoid.yml")
6
+ Mongoid.logger.level = old_level
7
+
8
+ options[:reload] = true # finalizes the mongodb database switch
9
+ end
10
+ end
@@ -0,0 +1,15 @@
1
+ module RailsEnvSwitcher::Switcher::Rails
2
+ def self.switch_env(old_env, env, options={})
3
+ if ::Rails.env != env
4
+ ENV['RAILS_ENV'] = env
5
+ ::Rails.env = env
6
+
7
+ Kernel.silence_warnings do
8
+ Dir[Rails.root.join('config', 'initializers', '*.rb')].map do |file|
9
+ load file
10
+ end
11
+ load Rails.root.join('config', 'environments', "#{env}.rb")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module RailsEnvSwitcher::Switcher::Reloader
2
+ def self.switch_env(old_env, env, options={})
3
+ # delete because we don't necessarily want to reload when we go back to
4
+ # the original environment
5
+ if options.delete(:reload)
6
+ reload!(false) if respond_to?(:reload!)
7
+ FactoryGirl.reload if defined?(FactoryGirl)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RailsEnvSwitcher
2
+ VERSION = '0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-env-switcher
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicolas Viennot
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description: Switch from a Rails environment to another
31
+ email:
32
+ - nicolas@viennot.biz
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/rails-env-switcher/switcher/active_record.rb
38
+ - lib/rails-env-switcher/switcher/copycopter_client.rb
39
+ - lib/rails-env-switcher/switcher/rails.rb
40
+ - lib/rails-env-switcher/switcher/mongoid.rb
41
+ - lib/rails-env-switcher/switcher/bundler.rb
42
+ - lib/rails-env-switcher/switcher/reloader.rb
43
+ - lib/rails-env-switcher/version.rb
44
+ - lib/rails-env-switcher/switcher.rb
45
+ - lib/rails-env-switcher/pry.rb
46
+ - lib/rails-env-switcher.rb
47
+ - README.md
48
+ homepage: http://github.com/nviennot/rails-env-switcher
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Switch from a Rails environment to another
72
+ test_files: []
73
+ has_rdoc: false