/usr/lib/ruby/vendor_ruby/rspec/support/recursive_const_methods.rb is in ruby-rspec-support 3.5.0c3e0m0s0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | module RSpec
module Support
# Provides recursive constant lookup methods useful for
# constant stubbing.
module RecursiveConstMethods
# We only want to consider constants that are defined directly on a
# particular module, and not include top-level/inherited constants.
# Unfortunately, the constant API changed between 1.8 and 1.9, so
# we need to conditionally define methods to ignore the top-level/inherited
# constants.
#
# Given:
# class A; B = 1; end
# class C < A; end
#
# On 1.8:
# - C.const_get("Hash") # => ::Hash
# - C.const_defined?("Hash") # => false
# - C.constants # => ["B"]
# - None of these methods accept the extra `inherit` argument
# On 1.9:
# - C.const_get("Hash") # => ::Hash
# - C.const_defined?("Hash") # => true
# - C.const_get("Hash", false) # => raises NameError
# - C.const_defined?("Hash", false) # => false
# - C.constants # => [:B]
# - C.constants(false) #=> []
if Module.method(:const_defined?).arity == 1
def const_defined_on?(mod, const_name)
mod.const_defined?(const_name)
end
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
def constants_defined_on(mod)
mod.constants.select { |c| const_defined_on?(mod, c) }
end
else
def const_defined_on?(mod, const_name)
mod.const_defined?(const_name, false)
end
def get_const_defined_on(mod, const_name)
mod.const_get(const_name, false)
end
def constants_defined_on(mod)
mod.constants(false)
end
end
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
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].join('::')]
end
end
def normalize_const_name(const_name)
const_name.sub(/\A::/, '')
end
end
end
end
|