NestedMultimap allows values to be assoicated with a nested set of keys.
Pushes the given object on to the end of all the containers.
map = NestedMultimap["a" => [100], "b" => [200, 300]] map << 300 map["a"] #=> [100, 300] map["c"] #=> [300]
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 47 def <<(value) @hash.each_value { |container| container << value } self.default << value self end
Retrieves the value object corresponding to the *keys object.
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 59 def [](*keys) i, l, r, k = 0, keys.length, self, self.class while r.is_a?(k) r = i < l ? r._internal_hash[keys[i]] : r.default i += 1 end r end
Returns a new array populated with all the containers from map including the default.
map = NestedMultimap.new map["a"] = 100 map["a", "b"] = 101 map["a"] = 102 map.containers_with_default #=> [[100, 101, 102], [100, 102], []]
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 133 def containers_with_default containers = [] each_container_with_default { |container| containers << container } containers end
Calls block once for each key/container in map, passing the key and container to the block as parameters.
map = NestedMultimap.new map["a"] = 100 map["a", "b"] = 101 map["a"] = 102 map["c"] = 200 map.each_association { |key, container| puts "#{key} is #{container}" }
produces:
["a", "b"] is [100, 101, 102] "c" is [200]
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 85 def each_association super() do |key, container| if container.respond_to?(:each_association) container.each_association do |nested_key, value| yield [key, nested_key].flatten, value end else yield key, container end end end
Calls block for every container in map including the default, passing the container as a parameter.
map = NestedMultimap.new map["a"] = 100 map["a", "b"] = 101 map["a"] = 102 map.each_container_with_default { |container| puts container }
produces:
[100, 101, 102] [100, 102] []
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 114 def each_container_with_default(&block) @hash.each_value do |container| iterate_over_container(container, &block) end iterate_over_container(default, &block) self end
Associates the value given by value with multiple key given by keys.
map = NestedMultimap.new map["a"] = 100 map["a", "b"] = 101 map["a"] = 102 map #=> {"a"=>{"b"=>[100, 101, 102], default => [100, 102]}}
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 18 def store(*args) keys = args value = args.pop raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value if keys.length > 1 update_container(keys.shift) do |container| container = self.class.new(container) unless container.is_a?(self.class) container[*keys] = value container end elsif keys.length == 1 super(keys.first, value) else self << value end end