1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Save before major refactor to split up Pry into pry_class and pry_instance

This commit is contained in:
John Mair 2010-12-26 02:51:34 +13:00
parent 294d46c02a
commit f11806d2da
3 changed files with 112 additions and 128 deletions

View file

@ -8,15 +8,21 @@ require "#{direc}/pry/version"
require "#{direc}/pry/input"
require "#{direc}/pry/output"
require "#{direc}/pry/commands"
require "#{direc}/pry/prompts"
class Pry
def self.start(target=TOPLEVEL_BINDING, options={})
options = {
:input => Pry.input,
:output => Pry.output
}.merge!(options)
new(options[:input], options[:output]).repl(target)
# class accessors
class << self
attr_reader :nesting
attr_accessor :last_result
attr_accessor :input, :output
attr_accessor :commands
attr_accessor :default_prompt, :wait_prompt
end
def self.start(target=TOPLEVEL_BINDING, options={})
new(options).repl(target)
end
def self.view(obj)
@ -31,32 +37,9 @@ class Pry
def self.reset_defaults
self.input = Input.new
self.output = Output.new
self.commands = Commands.new(self.output)
self.default_prompt = proc do |v, nest|
if nest == 0
"pry(#{Pry.view(v)})> "
else
"pry(#{Pry.view(v)}):#{Pry.view(nest)}> "
end
end
self.wait_prompt = proc do |v, nest|
if nest == 0
"pry(#{Pry.view(v)})* "
else
"pry(#{Pry.view(v)}):#{Pry.view(nest)}* "
end
end
end
# class accessors
class << self
attr_reader :nesting
attr_accessor :last_result
attr_accessor :default_prompt, :wait_prompt
attr_accessor :input, :output
attr_accessor :commands
self.commands = COMMANDS
self.default_prompt = DEFAULT_PROMPT
self.wait_prompt = WAIT_PROMPT
end
self.reset_defaults
@ -68,17 +51,25 @@ class Pry
end
attr_accessor :input, :output
attr_accessor :default_prompt, :wait_prompt
attr_accessor :commands
attr_accessor :default_prompt, :wait_prompt
attr_reader :last_result
def initialize(input = Pry.input, output = Pry.output)
@input = input
@output = output
def initialize(options={})
@default_prompt = Pry.default_prompt
@wait_prompt = Pry.wait_prompt
@commands = Commands.new(output)
options = {
:input => Pry.input,
:output => Pry.output,
:commands => Pry.commands,
:default_prompt => Pry.default_prompt,
:wait_prompt => Pry.wait_prompt
}.merge!(options)
@input = options[:input]
@output = options[:output]
@commands = options[:commands]
@default_prompt = options[:default_prompt]
@wait_prompt = options[:wait_prompt]
end
def nesting
@ -151,8 +142,9 @@ class Pry
def process_commands(val, eval_string, target)
def eval_string.clear() replace("") end
if action = commands.commands.find { |k, v| Array(k).any? { |a| a === val } }
pattern, action = commands.find { |k, v| Array(k).any? { |a| a === val } }
if pattern
options = {
:captures => $~ ? $~.captures : nil,
:eval_string => eval_string,
@ -162,7 +154,7 @@ class Pry
:output => output
}
action.last.call(options)
action.call(options)
end
end
@ -208,8 +200,8 @@ class Pry
end
module ObjectExtensions
def pry(target=self)
Pry.start(target)
def pry(target=self, options={})
Pry.start(target, options)
end
def __binding__

View file

@ -1,82 +1,74 @@
class Pry
class Commands
attr_reader :commands, :output
def initialize(out)
@output = out
@commands = {
["exit_program", "quit_program"] => proc { opts[:output].exit_program; exit },
"!" => proc { |opts| opts[:output].refresh; opts[:eval_string].clear },
"help" => proc { |opts| opts[:output].show_help; opts[:eval_string].clear },
"nesting" => proc { |opts| opts[:output].show_nesting(opts[:nesting]); opts[:eval_string].clear },
"status" => proc do |opts|
opts[:output].show_status(opts[:nesting], opts[:target])
opts[:eval_string].clear
end,
"exit_all" => proc { throw(:breakout, 0) },
["exit", "quit", "back", "cd .."] => proc do |opts|
output.exit
throw(:breakout, opts[:nesting].level)
end,
"ls" => proc do |opts|
opts[:output].ls(opts[:target])
opts[:eval_string].clear
end,
/^cat\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
opts[:output].cat(opts[:target], obj)
opts[:eval_string].clear
end,
/^cd\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
opts[:target].eval("#{obj}.pry")
opts[:output].cd obj
opts[:eval_string].clear
end,
/^show_doc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("method(:#{meth_name})").comment
opts[:output].show_doc doc
opts[:eval_string].clear
end,
/^show_idoc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("instance_method(:#{meth_name})").comment
opts[:output].show_doc doc
opts[:eval_string].clear
end,
/^show_method\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("method(:#{meth_name})").source
opts[:output].show_method code
opts[:eval_string].clear
end,
/^show_imethod\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("instance_method(:#{meth_name})").source
output.show_method code
opts[:eval_string].clear
end,
/^jump_to\s*(\d*)/ => proc do |opts|
break_level = opts[:captures].first.to_i
opts[:output].jump_to(break_level)
nesting = opts[:nesting]
COMMANDS = {
["exit_program", "quit_program"] => proc { opts[:output].exit_program; exit },
"!" => proc { |opts| opts[:output].refresh; opts[:eval_string].clear },
"help" => proc { |opts| opts[:output].show_help; opts[:eval_string].clear },
"nesting" => proc { |opts| opts[:output].show_nesting(opts[:nesting]); opts[:eval_string].clear },
"status" => proc do |opts|
opts[:output].show_status(opts[:nesting], opts[:target])
opts[:eval_string].clear
end,
"exit_all" => proc { throw(:breakout, 0) },
["exit", "quit", "back", "cd .."] => proc do |opts|
output.exit
throw(:breakout, opts[:nesting].level)
end,
"ls" => proc do |opts|
opts[:output].ls(opts[:target])
opts[:eval_string].clear
end,
/^cat\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
opts[:output].cat(opts[:target], obj)
opts[:eval_string].clear
end,
/^cd\s+(.+)/ => proc do |opts|
obj = opts[:captures].first
opts[:target].eval("#{obj}.pry")
opts[:output].cd obj
opts[:eval_string].clear
end,
/^show_doc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("method(:#{meth_name})").comment
opts[:output].show_doc doc
opts[:eval_string].clear
end,
/^show_idoc\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
doc = opts[:target].eval("instance_method(:#{meth_name})").comment
opts[:output].show_doc doc
opts[:eval_string].clear
end,
/^show_method\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("method(:#{meth_name})").source
opts[:output].show_method code
opts[:eval_string].clear
end,
/^show_imethod\s*(.+)/ => proc do |opts|
meth_name = opts[:captures].first
code = opts[:target].eval("instance_method(:#{meth_name})").source
opts[:output].show_method code
opts[:eval_string].clear
end,
/^jump_to\s*(\d*)/ => proc do |opts|
break_level = opts[:captures].first.to_i
opts[:output].jump_to(break_level)
nesting = opts[:nesting]
case break_level
when nesting.level
opts[:output].warn_already_at_level(nesting.level)
opts[:eval_string].clear
when (0...nesting.level)
throw(:breakout, break_level + 1)
else
opts[:output].err_invalid_nest_level(break_level,
nestingn.level - 1)
opts[:eval_string].clear
end
end
}
case break_level
when nesting.level
opts[:output].warn_already_at_level(nesting.level)
opts[:eval_string].clear
when (0...nesting.level)
throw(:breakout, break_level + 1)
else
opts[:output].err_invalid_nest_level(break_level,
nestingn.level - 1)
opts[:eval_string].clear
end
end
end
}
end

View file

@ -31,7 +31,7 @@ describe Pry do
output = OutputTester.new
o = Object.new
pry_tester = Pry.new(input, output)
pry_tester = Pry.new(:input => input, :output => output)
pry_tester.rep(o)
o.instance_variable_get(:@x).should == 10
end
@ -40,21 +40,21 @@ describe Pry do
output = OutputTester.new
o = Object.new
pry_tester = Pry.new(InputTester.new("self"), output)
pry_tester = Pry.new(:input => InputTester.new("self"), :output => output)
pry_tester.rep(o)
output.output_buffer.should == o
end
it 'should work with multi-line input' do
output = OutputTester.new
pry_tester = Pry.new(InputTester.new("x = ", "1 + 4"), output)
pry_tester = Pry.new(:input => InputTester.new("x = ", "1 + 4"), :output => output)
pry_tester.rep(Hello)
output.output_buffer.should == 5
end
it 'should define a nested class under Hello and not on top-level or Pry' do
output = OutputTester.new
pry_tester = Pry.new(InputTester.new("class Nested", "end"), output)
pry_tester = Pry.new(:input => InputTester.new("class Nested", "end"), :output => output)
pry_tester.rep(Hello)
Hello.const_defined?(:Nested).should == true
end
@ -69,7 +69,7 @@ describe Pry do
o = Object.new
pry_tester = Pry.new(input, output)
pry_tester = Pry.new(:input => input, :output => output)
pry_tester.repl(o)
o.instance_variable_get(:@x).should == 10
@ -135,7 +135,7 @@ describe Pry do
output = OutputTester.new
o = Class.new
pry_tester = Pry.new(input, output)
pry_tester = Pry.new(:input => input, :output => output)
pry_tester.repl(o)
output.#{meth}_invoked.should == true
@ -157,7 +157,7 @@ describe Pry do
output = OutputTester.new
o = Class.new
pry_tester = Pry.new(input, output)
pry_tester = Pry.new(:input => input, :output => output)
pry_tester.repl(o)
if "#{command}" != "!"