Module: RSpec::Support::RecursiveConstMethods
- Defined in:
- lib/rspec/support/recursive_const_methods.rb
Overview
Provides recursive constant lookup methods useful for constant stubbing.
Instance Method Summary collapse
- #const_defined_on?(mod, const_name) ⇒ Boolean
- #constants_defined_on(mod) ⇒ Object
- #get_const_defined_on(mod, const_name) ⇒ Object
- #normalize_const_name(const_name) ⇒ Object
- #recursive_const_defined?(const_name) ⇒ Boolean
- #recursive_const_get(const_name) ⇒ Object
Instance Method Details
#const_defined_on?(mod, const_name) ⇒ Boolean
29 30 31 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 29 def const_defined_on?(mod, const_name) mod.const_defined?(const_name) end |
#constants_defined_on(mod) ⇒ Object
39 40 41 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 39 def constants_defined_on(mod) mod.constants.select { |c| const_defined_on?(mod, c) } end |
#get_const_defined_on(mod, const_name) ⇒ Object
33 34 35 36 37 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 33 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) ⇒ Object
71 72 73 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 71 def normalize_const_name(const_name) const_name.sub(/\A::/, '') end |
#recursive_const_defined?(const_name) ⇒ Boolean
62 63 64 65 66 67 68 69 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 62 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) ⇒ Object
56 57 58 59 60 |
# File 'lib/rspec/support/recursive_const_methods.rb', line 56 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 |