summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConrad Irwin <conrad.irwin@gmail.com>2013-03-28 00:31:04 -0700
committerConrad Irwin <conrad.irwin@gmail.com>2013-03-28 01:02:13 -0700
commit29397efb371b34cefbf7c1f1d30b38f59888b49b (patch)
tree88ef96fa58bc106933d13513c4d4627992d128f1
parentda1283b0009ee052c41a3b3b2659949dff794b7b (diff)
downloadpry-29397efb371b34cefbf7c1f1d30b38f59888b49b.tar.gz
Modify methods using Pry::Method#redefine
I considered calling redefine source=, but Pry::Method objects are not designed to be mutable.
-rw-r--r--lib/pry/commands/edit.rb2
-rw-r--r--lib/pry/method.rb6
-rw-r--r--lib/pry/method/patcher.rb17
-rw-r--r--spec/method/patcher_spec.rb27
4 files changed, 40 insertions, 12 deletions
diff --git a/lib/pry/commands/edit.rb b/lib/pry/commands/edit.rb
index 6e8a806a..08ccd515 100644
--- a/lib/pry/commands/edit.rb
+++ b/lib/pry/commands/edit.rb
@@ -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
diff --git a/lib/pry/method.rb b/lib/pry/method.rb
index bdb589f7..acf91e46 100644
--- a/lib/pry/method.rb
+++ b/lib/pry/method.rb
@@ -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?
diff --git a/lib/pry/method/patcher.rb b/lib/pry/method/patcher.rb
index 687b46ce..07dfa8fb 100644
--- a/lib/pry/method/patcher.rb
+++ b/lib/pry/method/patcher.rb
@@ -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.
diff --git a/spec/method/patcher_spec.rb b/spec/method/patcher_spec.rb
new file mode 100644
index 00000000..14d59441
--- /dev/null
+++ b/spec/method/patcher_spec.rb
@@ -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