Class: LookupTable
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#canonnicals ⇒ Object
readonly
Returns the value of attribute canonnicals.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#ignore_case ⇒ Object
readonly
Returns the value of attribute ignore_case.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #alias(key:, to:) ⇒ Object
- #canonical(key) ⇒ Object
- #canonical_keys ⇒ Object
- #fuzzy_lookup_canonical(key) ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(ignore_case: false) ⇒ LookupTable
constructor
A new instance of LookupTable.
-
#keys(*args, **kwargs, &block) ⇒ Object
Because case is ignorable we can’t automatically delegate all methods, only those without arguments.
- #lookup_canonical(key) ⇒ Object (also: #lookup_key)
- #to_h ⇒ Object
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
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
3 4 5 |
# File 'lib/vv/utility/lookup_table.rb', line 3 def aliases @aliases end |
#canonnicals ⇒ Object (readonly)
Returns the value of attribute canonnicals.
3 4 5 |
# File 'lib/vv/utility/lookup_table.rb', line 3 def canonnicals @canonnicals end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
3 4 5 |
# File 'lib/vv/utility/lookup_table.rb', line 3 def data @data end |
#ignore_case ⇒ Object (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_keys ⇒ Object
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
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
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_h ⇒ Object
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 |