123456789_123456789_123456789_123456789_123456789_

Module: RSpec::Support::RecursiveConstMethods

Overview

Provides recursive constant lookup methods useful for constant stubbing.

Instance Method Summary

Instance Method Details

#const_defined_on?(mod, const_name)

See additional method definition at line 31.

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 45

def const_defined_on?(mod, const_name)
  mod.const_defined?(const_name)
end

#constants_defined_on(mod)

See additional method definition at line 41.

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 53

def constants_defined_on(mod)
  mod.constants.select { |c| const_defined_on?(mod, c) }
end

#get_const_defined_on(mod, const_name)

See additional method definition at line 35.

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 49

def get_const_defined_on(mod, const_name)
  return mod.const_get(const_name) if const_defined_on?(mod, const_name)

  raise NameError, "uninitialized constant #{mod.name}::#{const_name}"
end

#normalize_const_name(const_name)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 73

def normalize_const_name(const_name)
  const_name.sub(/\A::/, '')
end

#recursive_const_defined?(const_name) ⇒ Boolean

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 64

def recursive_const_defined?(const_name)
  parts = normalize_const_name(const_name).split('::')
  parts.inject([Object, '']) do |(mod, full_name), name|
    yield(full_name, name) if block_given? && !(Module === mod)
    return false unless const_defined_on?(mod, name)
    [get_const_defined_on(mod, name), [mod.name, name].join('::')]
  end
end

#recursive_const_get(const_name)

[ GitHub ]

  
# File 'rspec-support/lib/rspec/support/recursive_const_methods.rb', line 58

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