123456789_123456789_123456789_123456789_123456789_

Class: RSpec::Mocks::Space Private

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
RSpec::Mocks::NestedSpace
Inherits: Object
Defined in: rspec-mocks/lib/rspec/mocks/space.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#any_instance_mutex (readonly)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 58

attr_reader :proxies, :any_instance_recorders, :proxy_mutex, :any_instance_mutex

#any_instance_recorders (readonly)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 58

attr_reader :proxies, :any_instance_recorders, :proxy_mutex, :any_instance_mutex

#proxies (readonly)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 58

attr_reader :proxies, :any_instance_recorders, :proxy_mutex, :any_instance_mutex

#proxy_mutex (readonly)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 58

attr_reader :proxies, :any_instance_recorders, :proxy_mutex, :any_instance_mutex

Instance Method Details

#any_instance_proxy_for(klass)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 103

def any_instance_proxy_for(klass)
  AnyInstance::Proxy.new(any_instance_recorder_for(klass), proxies_of(klass))
end

#any_instance_recorder_for(klass, only_return_existing = false)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 93

def any_instance_recorder_for(klass, only_return_existing=false)
  any_instance_mutex.synchronize do
    id = klass.__id__
    any_instance_recorders.fetch(id) do
      return nil if only_return_existing
      any_instance_recorder_not_found_for(id, klass)
    end
  end
end

#any_instance_recorder_not_found_for(id, klass) (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 184

def any_instance_recorder_not_found_for(id, klass)
  any_instance_recorders[id] = AnyInstance::Recorder.new(klass)
end

#any_instance_recorders_from_ancestry_of(object)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 131

def any_instance_recorders_from_ancestry_of(object)
  # Optimization: `any_instance` is a feature we generally
  # recommend not using, so we can often early exit here
  # without doing an O(N) linear search over the number of
  # ancestors in the object's class hierarchy.
  return [] if any_instance_recorders.empty?

  # We access the ancestors through the singleton class, to avoid calling
  # `class` in case `class` has been stubbed.
  (class << object; ancestors; end).map do |klass|
    any_instance_recorders[klass.__id__]
  end.compact
end

#class_proxy_with_callback_verification_strategy(object, strategy) (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 171

def class_proxy_with_callback_verification_strategy(object, strategy)
  if RSpec::Mocks.configuration.verify_partial_doubles?
    VerifyingPartialClassDoubleProxy.new(
      self,
      object,
      @expectation_ordering,
      strategy
    )
  else
    PartialClassDoubleProxy.new(self, object, @expectation_ordering)
  end
end

#constant_mutator_for(name)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 89

def constant_mutator_for(name)
  @constant_mutators.find { |m| m.full_constant_name == name }
end

#ensure_registered(object)

Alias for #proxy_for.

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 125

alias ensure_registered proxy_for

#id_for(object) (private)

See additional method definition at line 191.

[ GitHub ]

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

def id_for(object)
  id = object.__id__

  return id if object.equal?(::ObjectSpace._id2ref(id))
  # this suggests that object.__id__ is proxying through to some wrapped object

  object.instance_exec do
    @__id_for_rspec_mocks_space ||= ::SecureRandom.uuid
  end
end

#new_mutex (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 147

def new_mutex
  Support::ReentrantMutex.new
end

#new_scope

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 69

def new_scope
  NestedSpace.new(self)
end

#proxies_of(klass)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 107

def proxies_of(klass)
  proxies.values.select { |proxy| klass === proxy.object }
end

#proxy_for(object) Also known as: #ensure_registered

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 111

def proxy_for(object)
  proxy_mutex.synchronize do
    id = id_for(object)
    proxies.fetch(id) { proxy_not_found_for(id, object) }
  end
end

#proxy_not_found_for(id, object) (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 151

def proxy_not_found_for(id, object)
  proxies[id] = case object
                when NilClass   then ProxyForNil.new(@expectation_ordering)
                when TestDouble then object.__build_mock_proxy_unless_expired(@expectation_ordering)
                when Class
                  class_proxy_with_callback_verification_strategy(object, CallbackInvocationStrategy.new)
                else
                  if RSpec::Mocks.configuration.verify_partial_doubles?
                    VerifyingPartialDoubleProxy.new(object, @expectation_ordering)
                  else
                    PartialDoubleProxy.new(object, @expectation_ordering)
                  end
                end
end

#register_constant_mutator(mutator)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 85

def register_constant_mutator(mutator)
  @constant_mutators << mutator
end

#registered?(object) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 127

def registered?(object)
  proxies.key?(id_for object)
end

#reset_all

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 78

def reset_all
  proxies.each_value { |proxy| proxy.reset }
  any_instance_recorders.each_value { |recorder| recorder.stop_all_observation! }
  any_instance_recorders.clear
  @constant_mutators.reverse.each { |mut| mut.idempotently_reset }
end

#superclass_proxy_for(klass)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 118

def superclass_proxy_for(klass)
  proxy_mutex.synchronize do
    id = id_for(klass)
    proxies.fetch(id) { superclass_proxy_not_found_for(id, klass) }
  end
end

#superclass_proxy_not_found_for(id, object) (private)

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 166

def superclass_proxy_not_found_for(id, object)
  raise "superclass_proxy_not_found_for called with something that is not a class" unless Class === object
  proxies[id] = class_proxy_with_callback_verification_strategy(object, NoCallbackInvocationStrategy.new)
end

#verify_all

[ GitHub ]

  
# File 'rspec-mocks/lib/rspec/mocks/space.rb', line 73

def verify_all
  proxies.values.each { |proxy| proxy.verify }
  any_instance_recorders.each_value { |recorder| recorder.verify }
end