Allow Pry::Method::Patcher to work without _pry_

This commit is contained in:
Conrad Irwin 2013-03-28 00:18:56 -07:00
parent 7454d549e8
commit e43daac85c
3 changed files with 21 additions and 7 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(_pry_, code_object).perform_patch
Method::Patcher.new(code_object).perform_patch
else
raise NotImplementedError, "Cannot yet patch #{code_object} objects!"
end

View File

@ -266,7 +266,7 @@ class Pry
when :c
c_source
when :ruby
ruby_source
Patcher.new(self).cached_source || ruby_source
end
end

View File

@ -1,31 +1,45 @@
class Pry
class Method
class Patcher
attr_accessor :_pry_
attr_accessor :code_object
def initialize(_pry_, code_object)
@_pry_ = _pry_
@@source_cache = {}
def initialize(code_object)
@code_object = code_object
end
def cached_source
@@source_cache[code_object.source_file]
end
# perform the patch
def perform_patch
source = patched_code
if code_object.alias?
with_method_transaction do
_pry_.evaluate_ruby patched_code
cached_eval source
end
else
_pry_.evaluate_ruby patched_code
cached_eval source
end
end
private
def cached_eval(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(code_object.source.lines.to_a))
end
def cache_key
"pry!#{code_object.owner.object_id}!#{code_object.name}"
end
# Run some code ensuring that at the end target#meth_name will not have changed.
#
# When we're redefining aliased methods we will overwrite the method at the