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

View File

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

View File

@ -35,7 +35,7 @@ describe "Pry#input_stack" do
:output => @str_output, :output => @str_output,
:input_stack => stack) :input_stack => stack)
instance.binding_stack << binding instance.push_binding binding
stack.size.should == 2 stack.size.should == 2
instance.retrieve_line.should == ":alex\n" instance.retrieve_line.should == ":alex\n"
stack.size.should == 2 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 it 'should provide different values for successive block invocations' do
pry = Pry.new pry = Pry.new
pry.binding_stack << binding pry.push_binding binding
pry.add_sticky_local(:test_local) { rand } pry.add_sticky_local(:test_local) { rand }
value1 = pry.evaluate_ruby 'test_local' value1 = pry.evaluate_ruby 'test_local'
value2 = pry.evaluate_ruby 'test_local' value2 = pry.evaluate_ruby 'test_local'