Class: Rack::Mount::Multimap

Inherits:
NestedMultimap show all
Defined in:
lib/rack/mount/multimap.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NestedMultimap

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

Methods inherited from Multimap

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

Constructor Details

#initialize(default = []) ⇒ Multimap

Returns a new instance of Multimap.



16
17
18
19
# File 'lib/rack/mount/multimap.rb', line 16

def initialize(default = [])
  @fuzz = {}
  super
end

Class Method Details

.[](*args) ⇒ Object



10
11
12
13
14
# File 'lib/rack/mount/multimap.rb', line 10

def self.[](*args)
  map = super
  map.instance_variable_set('@fuzz', {})
  map
end

Instance Method Details

#average_heightObject



71
72
73
74
# File 'lib/rack/mount/multimap.rb', line 71

def average_height
  lengths = containers_with_default.map { |e| e.length }
  lengths.inject(0) { |sum, len| sum += len }.to_f / lengths.size
end

#freezeObject



59
60
61
62
63
# File 'lib/rack/mount/multimap.rb', line 59

def freeze
  @fuzz.clear
  @fuzz = nil
  super
end

#heightObject



67
68
69
# File 'lib/rack/mount/multimap.rb', line 67

def height
  containers_with_default.max { |a, b| a.length <=> b.length }.length
end

#initialize_copy(original) ⇒ Object



21
22
23
24
# File 'lib/rack/mount/multimap.rb', line 21

def initialize_copy(original)
  @fuzz = original.instance_variable_get('@fuzz').dup
  super
end

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

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/mount/multimap.rb', line 26

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

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

  unless key.respond_to?(:=~)
    raise ArgumentError, "unsupported key: #{args.first.inspect}"
  end

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

      self.default = self.class.new(default) unless default.is_a?(self.class)
      default[*keys.dup] = value
    end
  else
    super(*args)
  end
end