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'
Michael Granger <ged@FaerieMUD.org>
Mahlon E. Smith <mahlon@martini.nu>
Methods that understand case-insensitive keys
Auto-generate methods which call the given delegate
after normalizing their first argument via normalize_key
# File lib/mongrel2/table.rb, line 36
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
Create a new Mongrel2::Table
using the given hash
for initial values.
# File lib/mongrel2/table.rb, line 53
def initialize( initial_values={} )
@hash = {}
initial_values.each {|k,v| self.append(k => v) }
end
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.
# File lib/mongrel2/table.rb, line 83
def append( hash )
self.merge!( hash ) do |key,origval,newval|
[ origval, newval ].flatten
end
end
Enumerator for iterating over the table contents, yielding each as an RFC822 header.
# File lib/mongrel2/table.rb, line 104
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
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.
# File lib/mongrel2/table.rb, line 140
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
into the receiver.
# File lib/mongrel2/table.rb, line 129
def merge!( other_table, &merge_callback )
nhash = normalize_hash( other_table.to_hash )
@hash.merge!( nhash, &merge_callback )
end
Return the Table
as a hash.
# File lib/mongrel2/table.rb, line 122
def to_h
@hash.dup
end
Return the Table
as RFC822 headers in a String
# File lib/mongrel2/table.rb, line 91
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
Return an array containing the values associated with the given keys.
# File lib/mongrel2/table.rb, line 150
def values_at( *keys )
@hash.values_at( *(keys.collect {|k| normalize_key(k)}) )
end
Create a Proc that will act as a getter for the given key
# File lib/mongrel2/table.rb, line 187
def make_getter( key )
return Proc.new { self[key] }
end
Create a Proc that will act as a setter for the given key
# File lib/mongrel2/table.rb, line 181
def make_setter( key )
return Proc.new {|new_value| self[ key ] = new_value }
end
Proxy method: handle getting/setting headers via methods instead of the index operator.
# File lib/mongrel2/table.rb, line 161
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