123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Mocks::InstanceMethodStasher Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#method_defined_directly_on_klass?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 100

def method_defined_directly_on_klass?
  method_defined_on_klass? && method_owned_by_klass?
end

#method_is_stashed? (readonly)

See additional method definition at line 18.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 49

def method_is_stashed?
  @method_is_stashed
end

#method_owned_by_klass?Boolean (readonly, private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 109

def method_owned_by_klass?
  owner = @klass.instance_method(@method).owner

  # On Ruby 2.0.0+ the owner of a method on a class which has been
  # `prepend`ed may actually be an instance, e.g.
  # `#<MyClass:0x007fbb94e3cd10>`, rather than the expected `MyClass`.
  owner = owner.class unless Module === owner

  # On some 1.9s (e.g. rubinius) aliased methods
  # can report the wrong owner. Example:
  # class MyClass
  #   class << self
  #     alias alternate_new new
  #   end
  # end
  #
  # MyClass.owner(:alternate_new) returns `Class` when incorrect,
  # but we need to consider the owner to be `MyClass` because
  # it is not actually available on `Class` but is on `MyClass`.
  # Hence, we verify that the owner actually has the method defined.
  # If the given owner does not have the method defined, we assume
  # that the method is actually owned by @klass.
  #
  # On 1.8, aliased methods can also report the wrong owner. Example:
  # module M
  #   def a; end
  #   module_function :a
  #   alias b a
  #   module_function :b
  # end
  # The owner of M.b is the raw Module object, instead of the expected
  # singleton class of the module
  return true if RUBY_VERSION < '1.9' && owner == @object
  owner == @klass || !(method_defined_on_klass?(owner))
end

#original_method (readonly)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 14

attr_reader :original_method

Instance Method Details

#handle_restoration_failures

ruby 2.0.0-p247 and 2.0.0-p195 both have a bug that we can’t work around :(. bugs.ruby-lang.org/issues/8686

See additional method definition at line 79.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 91

def handle_restoration_failures
  yield
rescue TypeError
  RSpec.warn_with(
    "RSpec failed to properly restore a partial double (#{@object.inspect}) " \
    "to its original state due to a known bug in MRI 2.0.0-p195 & p247 " \
    "(https://bugs.ruby-lang.org/issues/8686). This object may remain " \
    "screwed up for the rest of this process. Please upgrade to 2.0.0-p353 or above.",
    :call_site => nil, :use_spec_location_as_call_site => true
  )
end

#method_defined_on_klass?(klass = @klass) ⇒ Boolean (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 105

def method_defined_on_klass?(klass=@klass)
  MethodReference.method_defined_at_any_visibility?(klass, @method)
end

#restore

See additional method definition at line 36.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 61

def restore
  return unless @method_is_stashed

  if @klass.__send__(:method_defined?, @method)
    @klass.__send__(:undef_method, @method)
  end
  @klass.__send__(:alias_method, @method, stashed_method_name)
  @klass.__send__(:remove_method, stashed_method_name)
  @method_is_stashed = false
end

#stash

See additional method definition at line 23.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 54

def stash
  return if !method_defined_directly_on_klass? || @method_is_stashed

  @klass.__send__(:alias_method, stashed_method_name, @method)
  @method_is_stashed = true
end

#stashed_method_name

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/instance_method_stasher.rb', line 31

def stashed_method_name
  "obfuscated_by_rspec_mocks__#{@method}"
end