aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2015-09-21 13:45:27 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2015-09-21 13:45:27 -0700
commitf9dda1567ea8d5b27bd9d66ac5a8b43dc67a6b7e (patch)
treefb5bfd624e1b482248785eb14a253730d8c3ea57 /actionpack
parent8325d4c4737aa26692a0693b1289932e78e617af (diff)
downloadrails-f9dda1567ea8d5b27bd9d66ac5a8b43dc67a6b7e.tar.gz
rails-f9dda1567ea8d5b27bd9d66ac5a8b43dc67a6b7e.tar.bz2
rails-f9dda1567ea8d5b27bd9d66ac5a8b43dc67a6b7e.zip
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.
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_dispatch/http/mime_type.rb38
-rw-r--r--actionpack/test/dispatch/mime_type_test.rb5
2 files changed, 27 insertions, 16 deletions
diff --git a/actionpack/lib/action_dispatch/http/mime_type.rb b/actionpack/lib/action_dispatch/http/mime_type.rb
index ebad0b2708..a25b6c8883 100644
--- a/actionpack/lib/action_dispatch/http/mime_type.rb
+++ b/actionpack/lib/action_dispatch/http/mime_type.rb
@@ -5,20 +5,29 @@ require 'active_support/core_ext/string/starts_ends_with'
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 @@ to:
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)
diff --git a/actionpack/test/dispatch/mime_type_test.rb b/actionpack/test/dispatch/mime_type_test.rb
index 32e3bded55..520864b7d9 100644
--- a/actionpack/test/dispatch/mime_type_test.rb
+++ b/actionpack/test/dispatch/mime_type_test.rb
@@ -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