Make new mime types first class [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4407 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 5240d7a8
......@@ -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
......@@ -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)
......
......@@ -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)
......@@ -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 == "" }
......
......@@ -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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册