mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make new mime types first class [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4407 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
5240d7a84b
commit
5e998d1ea0
3 changed files with 10 additions and 9 deletions
|
@ -94,7 +94,7 @@ module ActionController #:nodoc:
|
|||
# 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
|
||||
|
@ -132,12 +132,8 @@ module ActionController #:nodoc:
|
|||
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)
|
||||
|
|
|
@ -33,7 +33,8 @@ module Mime
|
|||
|
||||
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)
|
||||
|
@ -126,6 +127,7 @@ module Mime
|
|||
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 == "" }
|
||||
|
||||
|
|
|
@ -24,7 +24,10 @@ class MimeTypeTest < Test::Unit::TestCase
|
|||
|
||||
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
|
Loading…
Reference in a new issue