Module: RSpec::Mocks::RecursiveConstMethods Private

Included in:
Constant, ConstantMutator, ConstantMutator::BaseMutator
Defined in:
lib/rspec/mocks/mutate_const.rb

Overview

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

Provides recursive constant lookup methods useful for constant stubbing.

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) const_defined_on?(mod, const_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:

  • (Boolean)
30
31
32
# File 'lib/rspec/mocks/mutate_const.rb', line 30
def const_defined_on?(mod, const_name)
  mod.const_defined?(const_name)
end

- (Object) constants_defined_on(mod)

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.

42
43
44
# File 'lib/rspec/mocks/mutate_const.rb', line 42
def constants_defined_on(mod)
  mod.constants.select { |c| const_defined_on?(mod, c) }
end

- (Object) get_const_defined_on(mod, const_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.

Raises:

  • (NameError)
34
35
36
37
38
39
40
# File 'lib/rspec/mocks/mutate_const.rb', line 34
def get_const_defined_on(mod, const_name)
  if const_defined_on?(mod, const_name)
    return mod.const_get(const_name)
  end
  raise NameError, "uninitialized constant #{mod.name}::#{const_name}"
end

- (Object) normalize_const_name(const_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.

73
74
75
# File 'lib/rspec/mocks/mutate_const.rb', line 73
def normalize_const_name(const_name)
  const_name.sub(/\A::/, '')
end

- (Boolean) recursive_const_defined?(const_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:

  • (Boolean)
65
66
67
68
69
70
71
# File 'lib/rspec/mocks/mutate_const.rb', line 65
def recursive_const_defined?(const_name)
  normalize_const_name(const_name).split('::').inject([Object, '']) do |(mod, full_name), name|
    yield(full_name, name) if block_given? && !mod.is_a?(Module)
    return false unless const_defined_on?(mod, name)
    [get_const_defined_on(mod, name), [mod, name].join('::')]
  end
end

- (Object) recursive_const_get(const_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.

59
60
61
62
63
# File 'lib/rspec/mocks/mutate_const.rb', line 59
def recursive_const_get(const_name)
  normalize_const_name(const_name).split('::').inject(Object) do |mod, name|
    get_const_defined_on(mod, name)
  end
end