Class: RSpec::Mocks::ConstantMutator::ConstantHider

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

Overview

Hides a defined constant for the duration of an example.

Instance Attribute Summary

Attributes inherited from BaseMutator

#full_constant_name, #original_value

Instance Method Summary (collapse)

Methods inherited from BaseMutator

#idempotently_reset, #initialize

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

This class inherits a constructor from RSpec::Mocks::ConstantMutator::BaseMutator

Instance Method Details

- (Object) mutate



232
233
234
235
236
237
238
# File 'lib/rspec/mocks/mutate_const.rb', line 232

def mutate
  return unless @defined = recursive_const_defined?(full_constant_name)
  @context = recursive_const_get(@context_parts.join('::'))
  @original_value = get_const_defined_on(@context, @const_name)

  @context.__send__(:remove_const, @const_name)
end

- (Object) reset



250
251
252
253
# File 'lib/rspec/mocks/mutate_const.rb', line 250

def reset
  return unless @defined
  @context.const_set(@const_name, @original_value)
end

- (Object) to_constant



240
241
242
243
244
245
246
247
248
# File 'lib/rspec/mocks/mutate_const.rb', line 240

def to_constant
  return Constant.unmutated(full_constant_name) unless @defined

  const = super
  const.hidden = true
  const.previously_defined = true

  const
end