diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-10-18 15:20:30 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-10-18 15:20:30 -0700 |
commit | 6ea781c9a7913ddf75ecdb26a273e194df95b2dc (patch) | |
tree | 2995c281a6202bc5975821c20e3c9f31f7a355e5 /actionpack/lib | |
parent | a3ad0a7b6718eef9462b519189cd67e2ca29bc28 (diff) | |
download | rails-6ea781c9a7913ddf75ecdb26a273e194df95b2dc.tar.gz rails-6ea781c9a7913ddf75ecdb26a273e194df95b2dc.tar.bz2 rails-6ea781c9a7913ddf75ecdb26a273e194df95b2dc.zip |
prefer composition over inheritence
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch/http/headers.rb | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/actionpack/lib/action_dispatch/http/headers.rb b/actionpack/lib/action_dispatch/http/headers.rb index a3bb25f75a..1580241b92 100644 --- a/actionpack/lib/action_dispatch/http/headers.rb +++ b/actionpack/lib/action_dispatch/http/headers.rb @@ -1,31 +1,35 @@ module ActionDispatch module Http - class Headers < ::Hash + class Headers + include Enumerable + @@env_cache = Hash.new { |h,k| h[k] = "HTTP_#{k.upcase.gsub(/-/, '_')}" } def initialize(*args) - - if args.size == 1 && args[0].is_a?(Hash) - super() - update(args[0]) - else - super - end + @headers = args.first || {} end def [](header_name) - super env_name(header_name) + @headers[env_name(header_name)] end + def []=(k,v); @headers[k] = v; end + def key?(k); @headers.key? k; end + alias :include? :key? + def fetch(header_name, default=nil, &block) - super env_name(header_name), default, &block + @headers.fetch env_name(header_name), default, &block + end + + def each(&block) + @headers.each(&block) end private # Converts a HTTP header name to an environment variable name if it is # not contained within the headers hash. def env_name(header_name) - include?(header_name) ? header_name : @@env_cache[header_name] + @headers.include?(header_name) ? header_name : @@env_cache[header_name] end end end |