提交 f9dda156 编写于 作者: A Aaron Patterson

change inheritance to composition

Changes `Mimes` to compose a set rather than inherit from array.  With
this change we don't need to define as many methods, so ISEQ memory is
saved.  Also it is clear which methods break the set cache.
上级 8325d4c4
......@@ -5,20 +5,29 @@
require 'active_support/deprecation'
module Mime
class Mimes < Array
def symbols
@symbols ||= map(&:to_sym)
class Mimes
include Enumerable
def initialize
@mimes = []
@symbols = nil
end
%w(<< concat shift unshift push pop []= clear compact! collect!
delete delete_at delete_if flatten! map! insert reject! reverse!
replace slice! sort! uniq!).each do |method|
module_eval <<-CODE, __FILE__, __LINE__ + 1
def #{method}(*)
@symbols = nil
super
end
CODE
def each
@mimes.each { |x| yield x }
end
def <<(type)
@mimes << type
@symbols = nil
end
def delete_if
@mimes.delete_if { |x| yield x }.tap { @symbols = nil }
end
def symbols
@symbols ||= map(&:to_sym)
end
end
......@@ -214,12 +223,13 @@ def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [], s
SET << new_mime
([string] + mime_type_synonyms).each { |str| LOOKUP[str] = SET.last } unless skip_lookup
([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = SET.last }
([string] + mime_type_synonyms).each { |str| LOOKUP[str] = new_mime } unless skip_lookup
([symbol] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext.to_s] = new_mime }
@register_callbacks.each do |callback|
callback.call(new_mime)
end
new_mime
end
def parse(accept_header)
......
......@@ -95,8 +95,9 @@ class MimeTypeTest < ActiveSupport::TestCase
test "custom type" do
begin
Mime::Type.register("image/foo", :foo)
assert_equal Mime::Type[:FOO], Mime::SET.last
type = Mime::Type.register("image/foo", :foo)
assert_equal Mime::Type[:FOO], type
assert Mime::Type.registered?(:FOO)
ensure
Mime::Type.unregister(:FOO)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册