123456789_123456789_123456789_123456789_123456789_

Module: YARD::CodeObjects::NamespaceMapper

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Defined in: lib/yard/code_objects/namespace_mapper.rb

Overview

This module controls registration and accessing of namespace separators for ::YARD::Registry lookup.

Since:

  • 0.9.1

Registering a Separator for a Namespace

Separator and Type Lookup Helpers

Invalidation callbacks

Class Attribute Details

.default_separatorString (rw, private)

Returns:

  • (String)

    the default separator when no separator can begin determined.

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 137

attr_accessor :default_separator

Class Method Details

.invalidatevoid (private)

This method returns an undefined value.

Invalidates all separators

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 125

def invalidate
  @map_match = nil
  (@invalidation_callbacks || []).each(&:call)
end

.mapHash (private)

Returns:

  • (Hash)

    a mapping of types to separators

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 114

def map
  @map ||= {}
end

.map_matchRegexp (private)

Returns:

  • (Regexp)

    the full list of separators as a regexp match

Since:

  • 0.9.1

[ GitHub ]

  
# 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)

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

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 107

def on_invalidate(&block)
  (@invalidation_callbacks ||= []).push(block)
end

.rev_mapHash (private)

Returns:

  • (Hash)

    a reverse mapping of separators to types

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 119

def rev_map
  @rev_map ||= {}
end

Instance Method Details

#clear_separatorsvoid

This method returns an undefined value.

Clears the map of separators.

Since:

  • 0.9.1

[ GitHub ]

  
# 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)

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

Examples:

default_separator "::"

Parameters:

  • value (String, nil) (defaults to: nil)

    the default separator, or nil to return the value

Since:

  • 0.9.1

[ GitHub ]

  
# 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)

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

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

Examples:

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

Parameters:

  • sep (String)

    the separator string for the namespace

  • 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.

See Also:

Since:

  • 0.9.1

[ GitHub ]

  
# 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

#separatorsArray<String>

Returns:

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 80

def separators
  NamespaceMapper.map.keys
end

#separators_for_type(type) ⇒ Array<Symbol>

Parameters:

  • type (String)

    the type to return separators for

Returns:

  • (Array<Symbol>)

    a list of separators registered to a type

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 97

def separators_for_type(type)
  NamespaceMapper.rev_map[type] || []
end

#separators_matchRegexp

Returns:

  • (Regexp)

    the regexp match of all separators

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 85

def separators_match
  NamespaceMapper.map_match
end

#types_for_separator(sep) ⇒ Array<Symbol>

Parameters:

  • sep (String)

    the separator to return types for

Returns:

  • (Array<Symbol>)

    a list of types registered to a separator

Since:

  • 0.9.1

[ GitHub ]

  
# File 'lib/yard/code_objects/namespace_mapper.rb', line 91

def types_for_separator(sep)
  NamespaceMapper.map[sep] || []
end

#unregister_separator_by_type(type)

Unregisters a separator by a type.

Parameters:

  • type (Symbol)

    the type to unregister

See Also:

Since:

  • 0.9.1

[ GitHub ]

  
# 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