module XfOOrth::SymbolMap

A module used to map strings to unique symbols

Attributes

forward_map[R]

Access to the mapping of names to symbols.

reverse_map[R]

Access to the mapping of symbols to names.

Public Class Methods

add_entry(name, presym=nil) click to toggle source

Add a global mapping for a string to a symbol that will not collide with existing symbols.
Parameters:

  • name - The string to be mapped.

  • presym - A pre-assigned symbol value or nil to generate a symbol.


Returns:

  • The symbol that corresponds to the name.


Endemic Code Smells

  • :reek:ControlParameter – false positive

# File lib/fOOrth/symbol_map.rb, line 31
def self.add_entry(name, presym=nil)
  @sync.synchronize do
    unless (symbol = @forward_map[name])
      symbol = presym || (@incrementer.succ!).to_sym
      connect(name, symbol)
    else
      error "F90: Attempt to redefine #{name}." if presym && presym != symbol
    end

    symbol
  end
end
map(name) click to toggle source

Get the entry for the mapping string. Return nil if there is no entry.
Parameters:

  • name - The string to be looked up.


Returns:

  • A symbol or nil if the symbol is not in the map.

# File lib/fOOrth/symbol_map.rb, line 49
def self.map(name)
  @forward_map[name]
end
map_info(name) click to toggle source

Get mapping info for a method name.

# File lib/fOOrth/library/introspection/symbol_map.rb, line 10
def self.map_info(name)
  symbol = map(name)
  target = symbol ? symbol.to_s : "not found."
  [symbol,  [["Name", name], ["Mapping", target]]]
end
restart(start) click to toggle source

Reset the incrementer to the given string. This used for testing only.
Parameters:

  • start - The new starting point of the generated symbols.


Note:

  • FOR TESTING ONLY.

# File lib/fOOrth/symbol_map.rb, line 67
def self.restart(start)
  @incrementer = start
end
unmap(mapped) click to toggle source

Get the entry for the mapping symbol. Return nil if there is no entry.
Parameters:

  • mapped - The mapping of the desired symbol.


Returns:

  • The name or nil if the symbol is not in the map.

# File lib/fOOrth/symbol_map.rb, line 58
def self.unmap(mapped)
  @reverse_map[mapped]
end

Private Class Methods

connect(name, symbol) click to toggle source

Set up the internal workings of the mapping hashes.

# File lib/fOOrth/symbol_map.rb, line 73
def self.connect(name, symbol)
  if (old = @reverse_map[symbol]) && (old != name)
    error "F90: Attempt to redefine #{name}."
  end

  @reverse_map[symbol] = name
  @forward_map[name] = symbol
end