aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/console.rb
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2012-02-01 13:36:47 +0100
committerPiotr Sarnacki <drogus@gmail.com>2012-02-16 20:41:35 +0100
commitdd655d88d660da8c094d20948ee721d29852f723 (patch)
treee227fd6a6ba69a76ea61858a9ffeafcf1e997344 /railties/lib/rails/commands/console.rb
parent951b58206255791587e2491435a80cf0ab3b797b (diff)
downloadrails-dd655d88d660da8c094d20948ee721d29852f723.tar.gz
rails-dd655d88d660da8c094d20948ee721d29852f723.tar.bz2
rails-dd655d88d660da8c094d20948ee721d29852f723.zip
Refactor Rails::Console to make it easier to test and add tests for it
Diffstat (limited to 'railties/lib/rails/commands/console.rb')
-rw-r--r--railties/lib/rails/commands/console.rb71
1 files changed, 46 insertions, 25 deletions
diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb
index e2956bbef6..86376ac7e6 100644
--- a/railties/lib/rails/commands/console.rb
+++ b/railties/lib/rails/commands/console.rb
@@ -4,48 +4,69 @@ require 'irb/completion'
module Rails
class Console
- def self.start(app)
- new(app).start
+ attr_reader :options, :app, :console, :arguments
+
+ def self.start(*args)
+ new(*args).start
end
- def initialize(app)
- @app = app
+ def initialize(app, arguments = ARGV)
+ @app = app
+ @arguments = arguments
+ app.load_console
+ @console = app.config.console || IRB
end
- def start
- options = {}
+ def options
+ @options ||= begin
+ options = {}
+
+ OptionParser.new do |opt|
+ opt.banner = "Usage: console [environment] [options]"
+ opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
+ opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
+ opt.parse!(arguments)
+ end
- OptionParser.new do |opt|
- opt.banner = "Usage: console [environment] [options]"
- opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v }
- opt.on("--debugger", 'Enable ruby-debugging for the console.') { |v| options[:debugger] = v }
- opt.parse!(ARGV)
+ options
end
+ end
- @app.sandbox = options[:sandbox]
- @app.load_console
+ def sandbox?
+ options[:sandbox]
+ end
- if options[:debugger]
- begin
- require 'ruby-debug'
- puts "=> Debugger enabled"
- rescue Exception
- puts "You need to install ruby-debug19 to run the console in debugging mode. With gems, use 'gem install ruby-debug19'"
- exit
- end
- end
+ def debugger?
+ options[:debugger]
+ end
+
+ def start
+ app.sandbox = sandbox?
+
+ require_debugger if debugger?
- if options[:sandbox]
+ if sandbox?
puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})"
puts "Any modifications you make will be rolled back on exit"
else
puts "Loading #{Rails.env} environment (Rails #{Rails.version})"
end
- console = Rails.application.config.console || IRB
- console::ExtendCommandBundle.send :include, Rails::ConsoleMethods
+ if defined?(console::ExtendCommandBundle)
+ console::ExtendCommandBundle.send :include, Rails::ConsoleMethods
+ end
console.start
end
+
+ def require_debugger
+ begin
+ require 'ruby-debug'
+ puts "=> Debugger enabled"
+ rescue Exception
+ puts "You need to install ruby-debug19 to run the console in debugging mode. With gems, use 'gem install ruby-debug19'"
+ exit
+ end
+ end
end
end