Class: RSpec::Mocks::Constant

Inherits:
Object
  • Object
show all
Extended by:
RecursiveConstMethods
Defined in:
lib/rspec/mocks/mutate_const.rb

Overview

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from RecursiveConstMethods

const_defined_on?, constants_defined_on, get_const_defined_on, normalize_const_name, recursive_const_defined?, recursive_const_get

Constructor Details

- (Constant) initialize(name)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Constant



85
86
87
88
89
90
# File 'lib/rspec/mocks/mutate_const.rb', line 85

def initialize(name)
  @name = name
  @previously_defined = false
  @stubbed = false
  @hidden = false
end

Instance Attribute Details

- (Object) hidden=(value) (writeonly)



101
102
103
# File 'lib/rspec/mocks/mutate_const.rb', line 101

def hidden=(value)
  @hidden = value
end

- (String) name (readonly)



93
94
95
# File 'lib/rspec/mocks/mutate_const.rb', line 93

def name
  @name
end

- (Object?) original_value



98
99
100
# File 'lib/rspec/mocks/mutate_const.rb', line 98

def original_value
  @original_value
end

- (Object) previously_defined=(value) (writeonly)



101
102
103
# File 'lib/rspec/mocks/mutate_const.rb', line 101

def previously_defined=(value)
  @previously_defined = value
end

- (Object) stubbed=(value) (writeonly)



101
102
103
# File 'lib/rspec/mocks/mutate_const.rb', line 101

def stubbed=(value)
  @stubbed = value
end

Class Method Details

+ (Constant) original(name)

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



149
150
151
152
# File 'lib/rspec/mocks/mutate_const.rb', line 149

def self.original(name)
  mutator = ::RSpec::Mocks.space.constant_mutator_for(name)
  mutator ? mutator.to_constant : unmutated(name)
end

+ (Object) unmutated(name)



134
135
136
137
138
139
140
141
142
# File 'lib/rspec/mocks/mutate_const.rb', line 134

def self.unmutated(name)
  const = new(name)
  const.previously_defined = recursive_const_defined?(name)
  const.stubbed = false
  const.hidden = false
  const.original_value = recursive_const_get(name) if const.previously_defined?

  const
end

Instance Method Details

- (Boolean) hidden?



123
124
125
# File 'lib/rspec/mocks/mutate_const.rb', line 123

def hidden?
  @hidden
end

- (Boolean) mutated?



111
112
113
# File 'lib/rspec/mocks/mutate_const.rb', line 111

def mutated?
  @stubbed || @hidden
end

- (Boolean) previously_defined?



105
106
107
# File 'lib/rspec/mocks/mutate_const.rb', line 105

def previously_defined?
  @previously_defined
end

- (Boolean) stubbed?



117
118
119
# File 'lib/rspec/mocks/mutate_const.rb', line 117

def stubbed?
  @stubbed
end

- (Object) to_s Also known as: inspect

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



128
129
130
# File 'lib/rspec/mocks/mutate_const.rb', line 128

def to_s
  "#<#{self.class.name} #{name}>"
end