module YARD::CodeObjects::NamespaceMapper

This module controls registration and accessing of namespace separators for {Registry} lookup.

@since 0.9.1

Attributes

default_separator[RW]

@return [String] the default separator when no separator can begin

determined.

Public Class Methods

invalidate() click to toggle source

Invalidates all separators @return [void]

# File lib/yard/code_objects/namespace_mapper.rb, line 125
def invalidate
  @map_match = nil
  (@invalidation_callbacks || []).each(&:call)
end
map() click to toggle source

@return [Hash] a mapping of types to separators

# File lib/yard/code_objects/namespace_mapper.rb, line 114
def map
  @map ||= {}
end
map_match() click to toggle source

@return [Regexp] the full list of separators as a regexp match

# File lib/yard/code_objects/namespace_mapper.rb, line 131
def map_match
  @map_match ||= map.keys.map {|k| Regexp.quote k }.join('|')
end
on_invalidate(&block) click to toggle source

Adds a callback that triggers when a new separator is registered or the cache is cleared by invalidation.

# File lib/yard/code_objects/namespace_mapper.rb, line 107
def on_invalidate(&block)
  (@invalidation_callbacks ||= []).push(block)
end
rev_map() click to toggle source

@return [Hash] a reverse mapping of separators to types

# File lib/yard/code_objects/namespace_mapper.rb, line 119
def rev_map
  @rev_map ||= {}
end

Public Instance Methods

clear_separators() click to toggle source

Clears the map of separators.

@return [void]

# File lib/yard/code_objects/namespace_mapper.rb, line 55
def clear_separators
  NamespaceMapper.invalidate
  NamespaceMapper.map = {}
  NamespaceMapper.rev_map = {}
end
default_separator(value = nil) click to toggle source

Gets or sets the default separator value to use when no separator for the namespace can be determined.

@param value [String, nil] the default separator, or nil to return the

value

@example

default_separator "::"
# File lib/yard/code_objects/namespace_mapper.rb, line 68
def default_separator(value = nil)
  if value
    NamespaceMapper.invalidate
    NamespaceMapper.default_separator = Regexp.quote value
  else
    NamespaceMapper.default_separator
  end
end
register_separator(sep, *valid_types) click to toggle source

Registers a separator with an optional set of valid types that must follow the separator lexically.

Calls all callbacks defined by {NamespaceMapper.on_invalidate} after the separator is registered.

@param sep [String] the separator string for the namespace @param valid_types [Array<Symbol>] a list of object types that

must follow the separator. If the list is empty, any type can
follow the separator.

@example Registering separators for a method object

# Anything after a "#" denotes a method object
register_separator "#", :method
# Anything after a "." denotes a method object
register_separator ".", :method

@see .on_invalidate

# File lib/yard/code_objects/namespace_mapper.rb, line 27
def register_separator(sep, *valid_types)
  NamespaceMapper.invalidate

  valid_types.each do |t|
    NamespaceMapper.rev_map[t] ||= []
    NamespaceMapper.rev_map[t] << sep
  end

  NamespaceMapper.map[sep] ||= []
  NamespaceMapper.map[sep] += valid_types
end
separators() click to toggle source

@return [Array<String>] all of the registered separators

# File lib/yard/code_objects/namespace_mapper.rb, line 80
def separators
  NamespaceMapper.map.keys
end
separators_for_type(type) click to toggle source

@param type [String] the type to return separators for @return [Array<Symbol>] a list of separators registered to a type

# File lib/yard/code_objects/namespace_mapper.rb, line 97
def separators_for_type(type)
  NamespaceMapper.rev_map[type] || []
end
separators_match() click to toggle source

@return [Regexp] the regexp match of all separators

# File lib/yard/code_objects/namespace_mapper.rb, line 85
def separators_match
  NamespaceMapper.map_match
end
types_for_separator(sep) click to toggle source

@param sep [String] the separator to return types for @return [Array<Symbol>] a list of types registered to a separator

# File lib/yard/code_objects/namespace_mapper.rb, line 91
def types_for_separator(sep)
  NamespaceMapper.map[sep] || []
end
unregister_separator_by_type(type) click to toggle source

Unregisters a separator by a type.

@param type [Symbol] the type to unregister @see register_separator

# File lib/yard/code_objects/namespace_mapper.rb, line 43
def unregister_separator_by_type(type)
  seps = NamespaceMapper.rev_map[type]
  return unless seps
  
  seps.each {|s| NamespaceMapper.map.delete(s) }
  NamespaceMapper.rev_map.delete(type)
  NamespaceMapper.invalidate
end