Class: LookupTable

Inherits:
Object show all
Defined in:
lib/vv/utility/lookup_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ignore_case: false) ⇒ LookupTable

Returns a new instance of LookupTable.



8
9
10
11
12
13
# 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

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



3
4
5
# File 'lib/vv/utility/lookup_table.rb', line 3

def aliases
  @aliases
end

#canonnicalsObject (readonly)

Returns the value of attribute canonnicals.



3
4
5
# File 'lib/vv/utility/lookup_table.rb', line 3

def canonnicals
  @canonnicals
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/vv/utility/lookup_table.rb', line 3

def data
  @data
end

#ignore_caseObject (readonly)

Returns the value of attribute ignore_case.



3
4
5
# File 'lib/vv/utility/lookup_table.rb', line 3

def ignore_case
  @ignore_case
end

Instance Method Details

#[](key) ⇒ Object



37
38
39
40
# File 'lib/vv/utility/lookup_table.rb', line 37

def [] key
  key = key.downcase if @ignore_case
  @data[ self.canonical key ]
end

#[]=(key, value) ⇒ Object



32
33
34
35
# 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:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



42
43
44
45
# File 'lib/vv/utility/lookup_table.rb', line 42

def canonical key
  key = key.downcase if @ignore_case
  @canonicals[key] || key
end

#canonical_keysObject



47
48
49
# File 'lib/vv/utility/lookup_table.rb', line 47

def canonical_keys
  @data.keys + (@aliases.keys - @data.keys)
end

#fuzzy_lookup_canonical(key) ⇒ Object

Raises:

  • (NotImplementedError)


88
89
90
91
92
# 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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# 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) ⇒ Object

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



69
70
71
72
73
74
75
76
77
# 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) ⇒ Object Also known as: lookup_key



79
80
81
82
83
84
85
# 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

#to_hObject



56
57
58
59
60
61
62
63
64
65
# 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