class LookupTable

Attributes

aliases[R]
canonnicals[R]
data[R]
ignore_case[R]

Public Class Methods

new(ignore_case: false) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 8
def initialize ignore_case: false
  @ignore_case = ignore_case
  @canonicals = Hash.new
  @aliases = Hash.new
  @data = Hash.new
end

Public Instance Methods

[](key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 37
def [] key
  key = key.downcase if @ignore_case
  @data[ self.canonical key ]
end
[]=(key, value) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 32
def []= key, value
  key = key.downcase if @ignore_case
  @data[ self.canonical key ] = value
end
alias( key:, to: ) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 15
def alias( key:, to: )

  if @ignore_case
    key = key.downcase
    to  =  to.downcase
  end

  return if key == to

  _ensure_alias_possible key

  @canonicals[key] = to

  @aliases[to] ||= Set.new
  @aliases[to] << key
end
canonical(key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 42
def canonical key
  key = key.downcase if @ignore_case
  @canonicals[key] || key
end
canonical_keys() click to toggle source
# File lib/vv/utility/lookup_table.rb, line 47
def canonical_keys
  @data.keys + (@aliases.keys - @data.keys)
end
fuzzy_lookup_canonical(key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 88
def fuzzy_lookup_canonical key
  raise NotImplementedError
  key = key.downcase if @ignore_case
  Dir.glob pattern
end
include?(key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 51
def include? key
  key = key.downcase if @ignore_case
  @data.include?( self.canonical key )
end
keys(*args, **kwargs, &block) click to toggle source

Because case is ignorable we can't automatically delegate all methods, only those without arguments

# File lib/vv/utility/lookup_table.rb, line 69
def keys *args, **kwargs, &block
  both = args.present? && kwargs.present?

  return to_h.keys(*args, **kwargs, &block) if both
  return to_h.keys(**kwargs, &block) if kwargs.present?
  return to_h.keys(*args, &block) if args.present?

  to_h.keys( &block )
end
lookup_canonical(key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 79
def lookup_canonical key
  key = key.downcase if @ignore_case

  return key if self.canonical_keys.include? key

  @canonicals[key]
end
Also aliased as: lookup_key
lookup_key(key)
Alias for: lookup_canonical
to_h() click to toggle source
# File lib/vv/utility/lookup_table.rb, line 56
def to_h
  keys = self.canonical_keys

  keys.inject({}) {|acc, key|
    data     = @data[key]    || {}
    aliases  = @aliases[key] || {}
    acc[key] = { data: data, aliases: aliases.to_a }
    acc
  }
end

Protected Instance Methods

_ensure_alias_possible(key) click to toggle source
# File lib/vv/utility/lookup_table.rb, line 96
def _ensure_alias_possible key
  return if @aliases[key].blank?
  aliases = @aliases[key]
  count = aliases.count
  message = \
  "Cannot alias `#{key}` because #{count} others currently alias it."
  fail message
end