Rename target to current_binding, add push_binding method

This commit is contained in:
Ryan Fitzgerald 2012-12-16 17:12:03 -08:00
parent 9ad2ec95a1
commit 2cfc4695f9
4 changed files with 21 additions and 18 deletions

View File

@ -116,10 +116,15 @@ class Pry
# The currently active `Binding`.
# @return [Binding] The currently active `Binding` for the session.
def current_context
def current_binding
binding_stack.last
end
alias target current_context
alias current_context current_binding # support previous API
# Push a binding for the given object onto the stack.
def push_binding(object)
binding_stack << Pry.binding_for(object)
end
# The current prompt.
# This is the prompt at the top of the prompt stack.
@ -164,11 +169,10 @@ class Pry
@output_array = Pry::HistoryArray.new(size)
end
# Inject all the sticky locals into the `target` binding.
# @param [Binding] target
# Inject all the sticky locals into the current binding.
def inject_sticky_locals!
sticky_locals.each_pair do |name, value|
inject_local(name, value, target)
inject_local(name, value, current_binding)
end
end
@ -271,11 +275,11 @@ class Pry
# @example
# Pry.new.r(Object.new)
def r(target_object = TOPLEVEL_BINDING)
binding_stack.push Pry.binding_for(target_object)
push_binding target_object
@eval_string = ""
loop do
throw(:breakout) if binding_stack.empty?
throw(:breakout) if current_binding.nil?
@suppress_output = false
inject_sticky_locals!
@ -299,7 +303,7 @@ class Pry
def accept_line(line)
begin
if !process_command_safely(line.lstrip, @eval_string, target)
if !process_command_safely(line.lstrip, @eval_string, current_binding)
@eval_string << "#{line.chomp}\n" unless line.empty?
end
rescue RescuableException => e
@ -343,8 +347,8 @@ class Pry
inject_sticky_locals!
exec_hook :before_eval, code, self
result = target.eval(code, Pry.eval_path, Pry.current_line)
set_last_result(result, target, code)
result = current_binding.eval(code, Pry.eval_path, Pry.current_line)
set_last_result(result, current_binding, code)
ensure
update_input_history(code)
exec_hook :after_eval, result, self
@ -393,8 +397,8 @@ class Pry
def retrieve_line(eval_string = '')
@indent.reset if eval_string.empty?
current_prompt = select_prompt(eval_string, target)
completion_proc = Pry.config.completer.build_completion_proc(target, self,
current_prompt = select_prompt(eval_string, current_binding)
completion_proc = Pry.config.completer.build_completion_proc(current_binding, self,
instance_eval(&custom_completions))
safe_completion_proc = proc{ |*a| Pry.critical_section{ completion_proc.call(*a) } }
@ -438,7 +442,7 @@ class Pry
end
# If the given line is a valid command, process it in the context of the
# current `eval_string` and context.
# current `eval_string` and binding.
# This method should not need to be invoked directly.
# @param [String] val The line to process.
# @param [String] eval_string The cumulative lines of input.

View File

@ -108,8 +108,7 @@ class PryTester
@history = options[:history]
if context
target = Pry.binding_for(context)
@pry.binding_stack << target
self.context = context
@pry.inject_sticky_locals!
end
@ -135,7 +134,7 @@ class PryTester
end
def context=(context)
@pry.binding_stack << Pry.binding_for(context)
@pry.push_binding context
end
# TODO: eliminate duplication with Pry#repl

View File

@ -35,7 +35,7 @@ describe "Pry#input_stack" do
:output => @str_output,
:input_stack => stack)
instance.binding_stack << binding
instance.push_binding binding
stack.size.should == 2
instance.retrieve_line.should == ":alex\n"
stack.size.should == 2

View File

@ -147,7 +147,7 @@ describe "Sticky locals (_file_ and friends)" do
it 'should provide different values for successive block invocations' do
pry = Pry.new
pry.binding_stack << binding
pry.push_binding binding
pry.add_sticky_local(:test_local) { rand }
value1 = pry.evaluate_ruby 'test_local'
value2 = pry.evaluate_ruby 'test_local'