class RSpec::Mocks::Constant

Provides information about constants that may (or may not) have been mutated by rspec-mocks.

Attributes

@private

@return [String] The fully qualified name of the constant.

@return [Object, nil] The original value (e.g. before it

was mutated by rspec-mocks) of the constant, or
nil if the constant was not previously defined.

@private

Public Class Methods

@api private

# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 11
def initialize(name)
  @name = name
  @previously_defined = false
  @stubbed = false
  @hidden = false
  @valid_name = true
  yield self if block_given?
end

Queries rspec-mocks to find out information about the named constant.

@param [String] name the name of the constant @return [Constant] an object containing information about the named

constant.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 86
def self.original(name)
  mutator = ::RSpec::Mocks.space.constant_mutator_for(name)
  mutator ? mutator.to_constant : unmutated(name)
end

@private

# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 68
def self.unmutated(name)
  previously_defined = !!recursive_const_defined?(name)
rescue NameError
  new(name) do |c|
    c.valid_name = false
  end
else
  new(name) do |const|
    const.previously_defined = previously_defined
    const.original_value = recursive_const_get(name) if previously_defined
  end
end

Public Instance Methods

@return [Boolean] Whether or not rspec-mocks has hidden

this constant.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 51
def hidden?
  @hidden
end
Alias for: to_s

@return [Boolean] Whether or not rspec-mocks has mutated

(stubbed or hidden) this constant.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 39
def mutated?
  @stubbed || @hidden
end

@return [Boolean] Whether or not the constant was defined

before the current example.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 33
def previously_defined?
  @previously_defined
end

@return [Boolean] Whether or not rspec-mocks has stubbed

this constant.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 45
def stubbed?
  @stubbed
end

The default ‘to_s` isn’t very useful, so a custom version is provided.

# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 62
def to_s
  "#<#{self.class.name} #{name}>"
end
Also aliased as: inspect

@return [Boolean] Whether or not the provided constant name

is a valid Ruby constant name.
# File rspec-mocks/lib/rspec/mocks/mutate_const.rb, line 57
def valid_name?
  @valid_name
end