Class: RSpec::Mocks::MethodReference

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb

Overview

Represents a method on an object that may or may not be defined. The method may be an instance method on a module or a method on any object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_reference, method_name) ⇒ MethodReference

Returns a new instance of MethodReference.



15
16
17
18
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 15

def initialize(object_reference, method_name)
  @object_reference = object_reference
  @method_name = method_name
end

Class Method Details

.for(object_reference, method_name) ⇒ Object



11
12
13
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 11

def self.for(object_reference, method_name)
  new(object_reference, method_name)
end

.instance_method_visibility_for(klass, method_name) ⇒ Object Also known as: method_defined_at_any_visibility?



67
68
69
70
71
72
73
74
75
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 67

def self.instance_method_visibility_for(klass, method_name)
  if klass.public_method_defined?(method_name)
    :public
  elsif klass.private_method_defined?(method_name)
    :private
  elsif klass.protected_method_defined?(method_name)
    :protected
  end
end

.method_visibility_for(object, method_name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 81

def self.method_visibility_for(object, method_name)
  vis = instance_method_visibility_for(class << object; self; end, method_name)

  # If the method is not defined on the class, `instance_method_visibility_for`
  # returns `nil`. However, it may be handled dynamically by `method_missing`,
  # so here we check `respond_to` (passing false to not check private methods).
  #
  # This only considers the public case, but I don't think it's possible to
  # write `method_missing` in such a way that it handles a dynamic message
  # with private or protected visibility. Ruby doesn't provide you with
  # the caller info.
  return vis unless vis.nil?

  proxy = RSpec::Mocks.space.proxy_for(object)
  respond_to = proxy.method_double_if_exists_for_message(:respond_to?)

  visible = respond_to && respond_to.original_method.call(method_name) ||
    object.respond_to?(method_name)

  return :public if visible
end

Instance Method Details

#defined?Boolean

A method is defined if we are able to get a ‘Method` object for it. In that case, we can assert against metadata like the arity.

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 46

def defined?
  @object_reference.when_loaded do |m|
    method_defined?(m)
  end
end

#implemented?Boolean

A method is implemented if sending the message does not result in a ‘NoMethodError`. It might be dynamically implemented by `method_missing`.

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 23

def implemented?
  @object_reference.when_loaded do |m|
    method_implemented?(m)
  end
end

#unimplemented?Boolean

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

This is not simply the inverse of ‘implemented?`: there are cases when we don’t know if a method is implemented and both ‘implemented?` and `unimplemented?` will return false.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 35

def unimplemented?
  @object_reference.when_loaded do |_m|
    return !implemented?
  end

  # If it's not loaded, then it may be implemented but we can't check.
  false
end

#visibilityObject



57
58
59
60
61
62
63
64
65
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 57

def visibility
  @object_reference.when_loaded do |m|
    return visibility_from(m)
  end

  # When it's not loaded, assume it's public. We don't want to
  # wrongly treat the method as private.
  :public
end

#with_signature {|Support::MethodSignature.new(original)| ... } ⇒ Object

Yields:



52
53
54
55
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/rspec-mocks-3.12.2/lib/rspec/mocks/method_reference.rb', line 52

def with_signature
  return unless (original = original_method)
  yield Support::MethodSignature.new(original)
end