class Mocha::AnyInstanceMethod

Public Instance Methods

define_new_method() click to toggle source
# File lib/mocha/any_instance_method.rb, line 33
    def define_new_method
      definition_target.class_eval(<<-CODE, __FILE__, __LINE__ + 1)
        def #{method}(*args, &block)
          self.class.any_instance.mocha.method_missing(:#{method}, *args, &block)
        end
      CODE
      return unless @original_visibility
      Module.instance_method(@original_visibility).bind(definition_target).call(method)
    end
hide_original_method() click to toggle source
# File lib/mocha/any_instance_method.rb, line 14
def hide_original_method
  return unless (@original_visibility = method_visibility(method))
  begin
    if RUBY_V2_PLUS
      @definition_target = PrependedModule.new
      stubbee.__send__ :prepend, @definition_target
    else
      @original_method = stubbee.instance_method(method)
      if @original_method && @original_method.owner == stubbee
        stubbee.send(:remove_method, method)
      end
    end
  # rubocop:disable Lint/HandleExceptions
  rescue NameError
    # deal with nasties like ActiveRecord::Associations::AssociationProxy
  end
  # rubocop:enable Lint/HandleExceptions
end
method_visibility(method) click to toggle source
# File lib/mocha/any_instance_method.rb, line 54
def method_visibility(method)
  (stubbee.public_instance_methods(true).include?(method) && :public) ||
    (stubbee.protected_instance_methods(true).include?(method) && :protected) ||
    (stubbee.private_instance_methods(true).include?(method) && :private)
end
mock() click to toggle source
# File lib/mocha/any_instance_method.rb, line 6
def mock
  stubbee.any_instance.mocha
end
remove_new_method() click to toggle source
# File lib/mocha/any_instance_method.rb, line 43
def remove_new_method
  definition_target.send(:remove_method, method)
end
reset_mocha() click to toggle source
# File lib/mocha/any_instance_method.rb, line 10
def reset_mocha
  stubbee.any_instance.reset_mocha
end
restore_original_method() click to toggle source
# File lib/mocha/any_instance_method.rb, line 47
def restore_original_method
  return if RUBY_V2_PLUS
  return unless @original_method && @original_method.owner == stubbee
  stubbee.send(:define_method, method, @original_method)
  Module.instance_method(@original_visibility).bind(stubbee).call(method)
end

Private Instance Methods

definition_target() click to toggle source
# File lib/mocha/any_instance_method.rb, line 62
def definition_target
  @definition_target ||= stubbee
end