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 47: def <<(value) 48: @hash.each_value { |container| container << value } 49: self.default << value 50: self 51: end
Retrieves the value object corresponding to the *keys object.
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 59 59: def [](*keys) 60: i, l, r, k = 0, keys.length, self, self.class 61: while r.is_a?(k) 62: r = i < l ? r._internal_hash[keys[i]] : r.default 63: i += 1 64: end 65: r 66: 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 133: def containers_with_default 134: containers = [] 135: each_container_with_default { |container| containers << container } 136: containers 137: 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 85: def each_association 86: super() do |key, container| 87: if container.respond_to?(:each_association) 88: container.each_association do |nested_key, value| 89: yield [key, nested_key].flatten, value 90: end 91: else 92: yield key, container 93: end 94: end 95: 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 114: def each_container_with_default(&block) 115: @hash.each_value do |container| 116: iterate_over_container(container, &block) 117: end 118: iterate_over_container(default, &block) 119: self 120: 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 18: def store(*args) 19: keys = args 20: value = args.pop 21: 22: raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value 23: 24: if keys.length > 1 25: update_container(keys.shift) do |container| 26: container = self.class.new(container) unless container.is_a?(self.class) 27: container[*keys] = value 28: container 29: end 30: elsif keys.length == 1 31: super(keys.first, value) 32: else 33: self << value 34: end 35: end
# File lib/rack/mount/vendor/multimap/nested_multimap.rb, line 144 144: def iterate_over_container(container) 145: if container.respond_to?(:each_container_with_default) 146: container.each_container_with_default do |value| 147: yield value 148: end 149: else 150: yield container 151: end 152: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.