Class: Mongrel2::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Loggability
Defined in:
lib/mongrel2/table.rb

Overview

The Mongrel2 Table class. Instances of this class provide a case-insensitive hash-like object that can store multiple values per key.

headers = Mongrel2::Table.new
headers['User-Agent'] = 'PornBrowser 1.1.5'
headers['user-agent']  # => 'PornBrowser 1.1.5'
headers[:user_agent]   # => 'PornBrowser 1.1.5'
headers.user_agent     # => 'PornBrowser 1.1.5'

Author/s

Constant Summary collapse

KEYED_METHODS =

Methods that understand case-insensitive keys

i[ [] []= delete fetch has_key? include? member? store ]
NON_DELEGATED_METHODS =

Method to not delegate to the inner hash

i[ inspect freeze ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_values = {}) ⇒ Table

Create a new Mongrel2::Table using the given hash for initial values.



57
58
59
60
# File 'lib/mongrel2/table.rb', line 57

def initialize( initial_values={} )
  @hash = {}
  initial_values.each {|k,v| self.append(k => v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (protected)

Proxy method: handle getting/setting headers via methods instead of the index operator.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/mongrel2/table.rb', line 182

def method_missing( sym, *args )
  # work magic
  return super unless sym.to_s =~ /^([a-z]\w+)(=)?$/

  # If it's an assignment, the (=)? will have matched
  key, assignment = $1, $2

  method_body = nil
  if assignment
    method_body = self.make_setter( key )
  else
    method_body = self.make_getter( key )
  end

  self.class.send( :define_method, sym, &method_body )
  return self.method( sym ).call( *args )
end

Class Method Details

.def_normalized_delegators(delegate, *syms) ⇒ Object

Auto-generate methods which call the given delegate after normalizing their first argument via normalize_key



40
41
42
43
44
45
46
47
48
# File 'lib/mongrel2/table.rb', line 40

def self::def_normalized_delegators( delegate, *syms )
  syms.each do |methodname|
    define_method( methodname ) do |key, *args|
      nkey = normalize_key( key )
      instance_variable_get( delegate ).
        __send__( methodname, nkey, *args )
    end
  end
end

Instance Method Details

#append(hash) ⇒ Object

Append the keys and values in the given hash to the table, transforming each value into an array if there was an existing value for the same key.



87
88
89
90
91
# File 'lib/mongrel2/table.rb', line 87

def append( hash )
  self.merge!( hash ) do |key,origval,newval|
    [ origval, newval ].flatten
  end
end

#each_header(&block) ⇒ Object

Enumerator for iterating over the table contents, yielding each as an RFC822 header.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mongrel2/table.rb', line 108

def each_header( &block )
  enum = Enumerator.new do |yielder|
    @hash.each do |header, value|
      Array( value ).each do |val|
        yielder.yield( normalize_header(header), val.to_s )
      end
    end
  end

  if block
    return enum.each( &block )
  else
    return enum
  end
end

#freezeObject

Overridden to freeze the inner hash as well.



170
171
172
173
# File 'lib/mongrel2/table.rb', line 170

def freeze
  super
  @hash.freeze
end

#initialize_copyObject

Make sure the inner Hash is unique on duplications.



64
65
66
# File 'lib/mongrel2/table.rb', line 64

def initialize_copy( * ) # :nodoc:
  @hash = deep_copy( @hash )
end

#inspectObject

Return a human-readable representation of the object suitable for debugging.



160
161
162
163
164
165
166
# File 'lib/mongrel2/table.rb', line 160

def inspect
  return "#<%p:%#x %p>" % [
    self.class,
    self.object_id * 2,
    @hash
  ]
end

#merge(other_table, &merge_callback) ⇒ Object Also known as: update

Return a new table which is the result of merging the receiver with other_table in the same fashion as Hash#merge. If the optional merge_callback block is provided, it is called whenever there is a key collision between the two.



144
145
146
147
148
# File 'lib/mongrel2/table.rb', line 144

def merge( other_table, &merge_callback ) # :yields: key, original_value, new_value
  other = self.dup
  other.merge!( other_table, &merge_callback )
  return other
end

#merge!(other_table, &merge_callback) ⇒ Object Also known as: update!

Merge other_table into the receiver.



133
134
135
136
# File 'lib/mongrel2/table.rb', line 133

def merge!( other_table, &merge_callback )
  nhash = normalize_hash( other_table.to_hash )
  @hash.merge!( nhash, &merge_callback )
end

#to_hObject Also known as: to_hash

Return the Table as a hash.



126
127
128
# File 'lib/mongrel2/table.rb', line 126

def to_h
  @hash.dup
end

#to_sObject

Return the Table as RFC822 headers in a String



95
96
97
98
99
100
101
102
103
104
# File 'lib/mongrel2/table.rb', line 95

def to_s
  @hash.collect do |header,value|
    Array( value ).collect {|val|
      "%s: %s" % [
        normalize_header( header ),
        val
      ]
    }
  end.flatten.sort.join( "\r\n" ) + "\r\n"
end

#values_at(*keys) ⇒ Object

Return an array containing the values associated with the given keys.



154
155
156
# File 'lib/mongrel2/table.rb', line 154

def values_at( *keys )
  @hash.values_at( *(keys.collect {|k| normalize_key(k)}) )
end