Parent

NestedMultimap

NestedMultimap allows values to be assoicated with a nested set of keys.

Public Instance Methods

multimap << obj => multimap click to toggle source

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
multimap[*keys] => value multimap[key1, key2, key3] => value click to toggle source

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
[]=(*args) click to toggle source
Alias for: store
containers_with_default => array click to toggle source

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
each_association { |key, container| block } => multimap click to toggle source

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
each_container_with_default { |container| block } => map click to toggle source

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
store(*keys, value) => value click to toggle source

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
Also aliased as: []=

Private Instance Methods

iterate_over_container(container) click to toggle source
     # 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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.