Class: RSpec::Mocks::InstanceMethodStasher

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/mocks/instance_method_stasher.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (InstanceMethodStasher) initialize(object, method)

Returns a new instance of InstanceMethodStasher



5
6
7
8
9
10
11
12
# File 'lib/rspec/mocks/instance_method_stasher.rb', line 5

def initialize(object, method)
  @object = object
  @method = method
  @klass = (class << object; self; end)

  @original_method = nil
  @method_is_stashed = false
end

Instance Attribute Details

- (Object) original_method (readonly)

Returns the value of attribute original_method



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

def original_method
  @original_method
end

Instance Method Details

- (Object) handle_restoration_failures

ruby 2.0.0-p247 has a bug that we can't work around :(. bugs.ruby-lang.org/issues/8686



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rspec/mocks/instance_method_stasher.rb', line 79

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-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

- (Boolean) method_is_stashed?

Returns:

  • (Boolean)


18
19
20
# File 'lib/rspec/mocks/instance_method_stasher.rb', line 18

def method_is_stashed?
  @method_is_stashed
end

- (Object) restore



61
62
63
64
65
66
67
68
69
70
# File '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

- (Object) stash



23
24
25
26
27
28
# File 'lib/rspec/mocks/instance_method_stasher.rb', line 23

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