Class: FuzzyNestedMultimap

Inherits:
NestedMultimap show all
Defined in:
lib/fuzzy_nested_multimap.rb

Overview

FuzzyNestedMultimap is an extension on top of NestedMultimap that allows fuzzy matching.

Constant Summary collapse

WILD_REGEXP =
/.*/.freeze

Instance Method Summary collapse

Methods inherited from NestedMultimap

#<<, #[], #containers_with_default, #each_association, #each_container_with_default, #height, #inspect

Methods inherited from Multimap

[], #containers, #delete, #each, #each_association, #each_container, #each_key, #each_pair, #each_value, #freeze, #has_value?, #index, #initialize, #initialize_copy, #invert, #keys, #merge, #replace, #select, #size, #to_a, #to_hash, #update, #values

Constructor Details

This class inherits a constructor from Multimap

Instance Method Details

#store(*args) ⇒ Object Also known as: []=

call-seq:

multimap[*keys] = value      => value
multimap.store(*keys, value) => value

Associates the value given by value with multiple key given by keys. Valid keys are restricted to strings and regexps. If a Regexp is used as a key, the value will be insert at every String key that matches that expression.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fuzzy_nested_multimap.rb', line 16

def store(*args)
  keys  = args.dup
  value = keys.pop
  key   = keys.shift || WILD_REGEXP

  raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value

  case key
  when Regexp
    if keys.empty?
      hash_each_pair { |k, l| l << value if key =~ k }
      self.default << value
    else
      hash_each_pair { |k, _|
        if key =~ k
          args[0] = k
          super(*args)
        end
      }

      self.default = self.class.new(default) unless default.is_a?(self.class)
      default[*keys.dup] = value
    end
  when String
    super(*args)
  else
    raise ArgumentError, "unsupported key: #{args.first.inspect}"
  end
end