Modify methods using Pry::Method#redefine

I considered calling redefine source=, but Pry::Method objects are not
designed to be mutable.
This commit is contained in:
Conrad Irwin 2013-03-28 00:31:04 -07:00
parent da1283b000
commit 29397efb37
4 changed files with 40 additions and 12 deletions

View File

@ -83,7 +83,7 @@ class Pry
ExceptionPatcher.new(_pry_, state, file_and_line_for_current_exception).perform_patch
else
if code_object.is_a?(Pry::Method)
Method::Patcher.new(code_object).perform_patch
code_object.redefine Pry::Editor.edit_tempfile_with_content(code_object.source)
else
raise NotImplementedError, "Cannot yet patch #{code_object} objects!"
end

View File

@ -270,6 +270,12 @@ class Pry
end
end
# Update the live copy of the method's source.
def redefine(source)
Patcher.new(self).patch_in_ram source
Pry::Method(owner.instance_method(name))
end
# Can we get the source code for this method?
# @return [Boolean]
def source?

View File

@ -14,30 +14,25 @@ class Pry
end
# perform the patch
def perform_patch
source = patched_code
def patch_in_ram(source)
if method.alias?
with_method_transaction do
cached_eval source
redefine source
end
else
cached_eval source
redefine source
end
end
private
def cached_eval(source)
def redefine(source)
@@source_cache[cache_key] = source
TOPLEVEL_BINDING.eval source, cache_key
end
def patched_code
@patched_code ||= wrap(Pry::Editor.edit_tempfile_with_content(method.source.lines.to_a))
TOPLEVEL_BINDING.eval wrap(source), cache_key
end
def cache_key
"pry!#{method.owner.object_id}!#{method.name}"
"pry-redefined(0x#{method.owner.object_id.to_s(16)}##{method.name})"
end
# Run some code ensuring that at the end target#meth_name will not have changed.

View File

@ -0,0 +1,27 @@
require 'helper'
describe Pry::Method::Patcher do
before do
@x = Object.new
def @x.test; :before; end
@method = Pry::Method(@x.method(:test))
end
it "should change the behaviour of the method" do
@x.test.should == :before
@method.redefine "def @x.test; :after; end\n"
@x.test.should == :after
end
it "should return a new method with new source" do
@method.source.strip.should == "def @x.test; :before; end"
@method.redefine("def @x.test; :after; end\n").
source.strip.should == "def @x.test; :after; end"
end
it "should change the source of new Pry::Method objects" do
@method.redefine "def @x.test; :after; end\n"
Pry::Method(@x.method(:test)).source.strip.should == "def @x.test; :after; end"
end
end