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,11 +1,5 @@
class Pry
class Commands
attr_reader :commands, :output
def initialize(out)
@output = out
@commands = {
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 },
@ -55,7 +49,7 @@ class Pry
/^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[:output].show_method code
opts[:eval_string].clear
end,
/^jump_to\s*(\d*)/ => proc do |opts|
@ -76,7 +70,5 @@ class Pry
end
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}" != "!"