class YARD::RegistryResolver
@since 0.9.1
@see Registry.resolve
directly.
Used by {Registry.resolve}, so there is no need to use this class
Handles all logic for complex lexical and inherited object resolution.
def collect_namespaces(object)
def collect_namespaces(object) return [] unless object.respond_to?(:inheritance_tree) nss = object.inheritance_tree(true) if object.respond_to?(:superclass) nss |= [P('Object')] if object.superclass != P('BasicObject') nss |= [P('BasicObject')] end nss end
def initialize(registry = Registry)
-
registry
(Registry
) -- only set this if customizing the registry
def initialize(registry = Registry) @registry = registry @default_sep = nil # Preload all code objects for separator declarations YARD::CodeObjects.constants.map {|t| YARD::CodeObjects.const_get(t) } end
def lookup_by_path(path, opts = {})
- Example: A lookup on a method through the inheritance tree -
Example: A lookup from the A::B namespace -
Example: A lookup from root -
Returns:
-
(CodeObjects::Base, CodeObjects::Proxy, nil)
- the first object
Options Hash:
(**opts)
-
type
(Symbol
) -- an optional type hint for the resolver -
proxy_fallback
(Boolean
) -- when true, a proxy is returned -
inheritance
(Boolean
) -- whether to perform lookups through -
namespace
(CodeObjects::Base, :root, nil
) -- the namespace
def lookup_by_path(path, opts = {}) path = path.to_s namespace = opts[:namespace] inheritance = opts[:inheritance] || false proxy_fallback = opts[:proxy_fallback] || false type = opts[:type] if namespace.is_a?(CodeObjects::Proxy) return proxy_fallback ? CodeObjects::Proxy.new(namespace, path, type) : nil end if namespace == :root || !namespace namespace = @registry.root else namespace = namespace.parent until namespace.is_a?(CodeObjects::NamespaceObject) end orignamespace = namespace if path =~ starts_with_default_separator_match path = $' namespace = @registry.root orignamespace = @registry.root end resolved = nil lexical_lookup = 0 while namespace && !resolved resolved = lookup_path_direct(namespace, path, type) resolved ||= lookup_path_inherited(namespace, path, type) if inheritance break if resolved namespace = namespace.parent lexical_lookup += 1 end # method objects cannot be resolved through lexical lookup by more than 1 ns if lexical_lookup > 1 && resolved.is_a?(CodeObjects::MethodObject) resolved = nil end if proxy_fallback resolved ||= CodeObjects::Proxy.new(orignamespace, path, type) end resolved end
def lookup_path_direct(namespace, path, type)
def lookup_path_direct(namespace, path, type) result = namespace.root? && validate(@registry.at(path), type) return result if result if path =~ starts_with_separator_match return validate(@registry.at(namespace.path + path), type) end separators.each do |sep| result = validate(@registry.at("#{namespace.path}#{sep}#{path}"), type) return result if result end nil end
def lookup_path_inherited(namespace, path, type)
def lookup_path_inherited(namespace, path, type) resolved = nil last_obj = namespace scopes = [] last_sep = nil pos = 0 if path =~ starts_with_separator_match last_sep = $1 path = $' end path.scan(split_on_separators_match).each do |part, sep| cur_obj = nil pos += part.length pos += sep.length parsed_end = pos == path.length if !last_obj || (!parsed_end && !last_obj.is_a?(CodeObjects::NamespaceObject)) break # can't continue end collect_namespaces(last_obj).each do |ns| next if ns.is_a?(CodeObjects::Proxy) found = nil search_seps = [] scopes.each do |scope| search_seps += separators_for_type(scope) end if search_seps.empty? search_seps = if ns.type == :root [""] elsif last_sep.nil? separators else [@default_sep] end end ([last_sep] | search_seps).compact.each do |search_sep| found = @registry.at(ns.path + search_sep.to_s + part) break if found end break cur_obj = found if found end last_sep = sep scopes = types_for_separator(sep) || [] last_obj = cur_obj resolved = cur_obj if parsed_end && cur_obj && (type.nil? || type == cur_obj.type) end resolved end
def split_on_separators_match
-
(Regexp)
- the regexp that can be used to split a string on all
def split_on_separators_match @@split_on_separators_match ||= /(.+?)(#{separators_match}|$)/ end
def starts_with_default_separator_match
-
(Regexp)
- the regexp match of the default separator
def starts_with_default_separator_match @@starts_with_default_separator_match ||= /\A#{default_separator}/ end
def starts_with_separator_match
-
(Regexp)
- the regexp that matches strings starting with
def starts_with_separator_match @@starts_with_separator_match ||= /\A(#{separators_match})/ end
def validate(obj, type)
def validate(obj, type) !type || (obj && obj.type == type) ? obj : nil end