Skip to content

Commit

Permalink
Make new mime types first class [DHH]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4407 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jun 2, 2006
1 parent 5240d7a commit 5e998d1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
10 changes: 3 additions & 7 deletions actionpack/lib/action_controller/mime_responds.rb
Expand Up @@ -94,7 +94,7 @@ module InstanceMethods
# and accept Rails' defaults, life will be much easier.
def respond_to(*types, &block)
raise ArgumentError, "respond_to takes either types or a block, never bot" unless types.any? ^ block
block ||= lambda { |responder| types.each { |type| responder.send(type) } }
block ||= lambda { |responder| types.each { |type| responder.known(type) } }
responder = Responder.new(block.binding)
block.call(responder)
responder.respond
Expand Down Expand Up @@ -132,12 +132,8 @@ def custom(mime_type, &block)
end
end

for mime_type in %w( all html js xml rss atom yaml )
eval <<-EOT
def #{mime_type}(&block)
custom(Mime::#{mime_type.upcase}, &block)
end
EOT
def known(mime_type_extension, &block)
custom(Mime.const_get(mime_type_extension.to_s.upcase), &block)
end

def any(*args, &block)
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/mime_type.rb
Expand Up @@ -33,7 +33,8 @@ def lookup(string)

def register(string, symbol, synonyms = [])
Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms)
LOOKUP[string] = Mime.send :const_get, symbol.to_s.upcase
SET << Mime.send(:const_get, symbol.to_s.upcase)
LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last
end

def parse(accept_header)
Expand Down Expand Up @@ -126,6 +127,7 @@ def ==(mime_type)
ATOM = Type.new "application/atom+xml", :atom
YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml )

SET = [ ALL, TEXT, HTML, JS, ICS, XML, RSS, ATOM, YAML ]

LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" }

Expand Down
5 changes: 4 additions & 1 deletion actionpack/test/controller/mime_type_test.rb
Expand Up @@ -24,7 +24,10 @@ def test_parse_with_q

def test_custom_type
Mime::Type.register("image/gif", :gif)
assert_nothing_raised { Mime::GIF }
assert_nothing_raised do
Mime::GIF
assert_equal Mime::GIF, Mime::SET.last
end
Mime.send :remove_const, :GIF
end
end

0 comments on commit 5e998d1

Please sign in to comment.