lib/bogus/stubbing/has_overwritten_methods.rb in bogus-0.1.4 vs lib/bogus/stubbing/has_overwritten_methods.rb in bogus-0.1.5

- old
+ new

@@ -1,25 +1,43 @@ +require 'set' + module Bogus module HasOverwritenMethods + def self.aliased_name(name) + :"__bogus__alias__#{name}" + end + + def self.alias(object, new_name, name) + object.singleton_class.send(:alias_method, new_name, name) + end + def __overwritten_methods__ - @__overwritten_methods__ ||= {} + @__overwritten_methods__ ||= Set.new end def __overwrite__(name, method, body) - return if __overwritten_methods__[name] - method = method.to_proc if method - __overwritten_methods__[name] = method || :no_such_method + return if __overwritten_methods__.include?(name) + + new_name = HasOverwritenMethods.aliased_name(name) + HasOverwritenMethods.alias(self, new_name, name) if method + + __overwritten_methods__ << name + instance_eval(body) end def __reset__ - __overwritten_methods__.each do |name, method| - method = __overwritten_methods__.delete(name) - instance_eval "undef #{name}" - next if method == :no_such_method - define_singleton_method(name, method) + __overwritten_methods__.each do |name| + new_name = HasOverwritenMethods.aliased_name(name) + + if respond_to?(new_name) + HasOverwritenMethods.alias(self, name, new_name) + instance_eval "undef #{new_name}" + else + instance_eval "undef #{name}" + end end - @__overwritten_methods__ = {} + @__overwritten_methods__ = nil @__shadow__ = nil end end end