123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Mocks::ClassNewMethodReference Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RSpec::Mocks::ObjectMethodReference
Defined in: rspec-mocks/lib/rspec/mocks/method_reference.rb

Overview

When a class’s ‘.new` method is stubbed, we want to use the method signature from #initialize because `.new`’s signature is a generic ‘def new(*args)` and it simply delegates to #initialize and forwards all args…so the method with the actually used signature is #initialize.

This method reference implementation handles that specific case.

Constant Summary

Class Method Summary

Instance Attribute Summary

MethodReference - Inherited

#defined?

A method is defined if we are able to get a ‘Method` object for it.

#implemented?

A method is implemented if sending the message does not result in a ‘NoMethodError`.

#unimplemented?

Returns true if we definitively know that sending the method will result in a ‘NoMethodError`.

Instance Method Summary

Class Method Details

.applies_to?(method_name) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/method_reference.rb', line 185

def self.applies_to?(method_name)
  return false unless method_name == :new
  klass = yield
  return false unless ::Class === klass && klass.respond_to?(:new, true)

  # We only want to apply our special logic to normal `new` methods.
  # Methods that the user has monkeyed with should be left as-is.
  uses_class_new?(klass)
end

.uses_class_new?(klass) ⇒ Boolean

Ruby 2’s Method#== is too strict

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/method_reference.rb', line 202

def self.uses_class_new?(klass)
  ::RSpec::Support.method_handle_for(klass, :new) == CLASS_NEW.bind(klass)
end

Instance Method Details

#with_signature

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/method_reference.rb', line 207

def with_signature
  @object_reference.when_loaded do |klass|
    yield Support::MethodSignature.new(klass.instance_method(:initialize))
  end
end