From c17b3301e6cd05ae347a2dfa59b4a34290185667 Mon Sep 17 00:00:00 2001 From: Austin Ziegler Date: Tue, 2 Jan 2024 00:49:40 -0500 Subject: [PATCH] More WIP, still does not work --- lib/mime/type.rb | 12 ++++++------ lib/mime/types.rb | 6 ++---- lib/mime/types/deprecations.rb | 2 +- test/test_mime_type.rb | 17 +++++++---------- test/test_mime_types_class.rb | 2 +- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/mime/type.rb b/lib/mime/type.rb index ec4fb67..0b3f747 100644 --- a/lib/mime/type.rb +++ b/lib/mime/type.rb @@ -190,7 +190,7 @@ def <=>(other) # consumers of mime-types. For the next major version of MIME::Types, this # method will become #<=> and #priority_compare will be removed. def priority_compare(other) - if (cmp = __sort_priority <=> other.__sort_priority).zero? + if (cmp = __sort_priority <=> other.__sort_priority) == 0 simplified <=> other.simplified else cmp @@ -689,15 +689,15 @@ def clear_sort_priority # 16, to a minimum of 0. def update_sort_priority extension_count = @extensions.length - obsolete = instance_variable_defined?(:@obsolete) && @obsolete ? 1 << 7 : 0 - provisional = instance_variable_defined?(:@provisional) && @provisional ? 1 << 6 : 0 - registered = instance_variable_defined?(:@registered) && @registered ? 0 : 1 << 5 + obsolete = (instance_variable_defined?(:@obsolete) && @obsolete) ? 1 << 7 : 0 + provisional = (instance_variable_defined?(:@provisional) && @provisional) ? 1 << 6 : 0 + registered = (instance_variable_defined?(:@registered) && @registered) ? 0 : 1 << 5 complete = extension_count.nonzero? ? 0 : 1 << 4 extension_count = [0, 16 - extension_count].max @__sort_priority = obsolete | registered | provisional | complete | extension_count - @__priority_penalty = (instance_variable_defined?(:@obsolete) && @obsolete ? 3 : 0) + - (instance_variable_defined?(:@registered) && @registered ? 0 : 2) + @__priority_penalty = ((instance_variable_defined?(:@obsolete) && @obsolete) ? 3 : 0) + + ((instance_variable_defined?(:@registered) && @registered) ? 0 : 2) end def __priority_penalty diff --git a/lib/mime/types.rb b/lib/mime/types.rb index b8e1377..a632a0b 100644 --- a/lib/mime/types.rb +++ b/lib/mime/types.rb @@ -151,10 +151,8 @@ def [](type_id, complete: false, registered: false) # puts MIME::Types.type_for(%w(citydesk.xml citydesk.gif)) # => [application/xml, image/gif, text/xml] def type_for(filename) - extensions = Array(filename).map { |fn| fn.chomp.downcase[/\.?([^.]*?)$/, 1] } - - extensions.flat_map { |ext| - @extension_index[ext] + Array(filename).flat_map { |fn| + @extension_index[fn.chomp.downcase[/\.?([^.]*?)\z/, 1]] }.compact.inject(Set.new, :+).sort { |a, b| by_ext = a.extension_priority(*extensions) <=> b.extension_priority(*extensions) diff --git a/lib/mime/types/deprecations.rb b/lib/mime/types/deprecations.rb index 05645a9..c2810ed 100644 --- a/lib/mime/types/deprecations.rb +++ b/lib/mime/types/deprecations.rb @@ -32,7 +32,7 @@ def self.deprecated(klass, sym, message = nil, &block) # :nodoc: " #{message}" end - MIME::Types.logger.warn <<-WARNING.chomp.strip + MIME::Types.logger.debug <<-WARNING.chomp.strip #{caller(2..2).first}: #{klass}#{level}#{sym}#{pre_message} is deprecated#{message}. WARNING diff --git a/test/test_mime_type.rb b/test/test_mime_type.rb index 43a91d3..0a0666a 100644 --- a/test/test_mime_type.rb +++ b/test/test_mime_type.rb @@ -265,27 +265,27 @@ def mime_type(content_type) end it "is true for an equivalent MIME::Type" do - assert text_plain.eql?(mime_type("text/Plain")) + assert text_plain.eql?(mime_type("content-type" => "text/Plain")) end it "is true for an equivalent subclass of MIME::Type" do subclass = Class.new(MIME::Type) - assert text_plain.eql?(subclass.new("text/plain")) + assert text_plain.eql?(subclass.new("content-type" => "text/plain")) end end describe "#hash" do it "is the same between #eql? MIME::Type instances" do - assert_equal text_plain.hash, mime_type("text/plain").hash + assert_equal text_plain.hash, mime_type("content-type" => "text/plain").hash end it "is the same between #eql? MIME::Type instances of different classes" do subclass = Class.new(MIME::Type) - assert_equal text_plain.hash, subclass.new("text/plain").hash + assert_equal text_plain.hash, subclass.new("content-type" => "text/plain").hash end it "uses the #simplified value" do - assert_equal text_plain.hash, mime_type("text/Plain").hash + assert_equal text_plain.hash, mime_type("content-type" => "text/Plain").hash end end @@ -322,11 +322,11 @@ def mime_type(content_type) describe "#priority_compare" do def priority(type) - "#{type} (#{("%08b" % type.__sort_priority).split(//).join(" ")})" + "#{type} (#{("%08b" % type.__sort_priority).chars.join(" ")})" end def assert_priority_less(left, right) - assert_equal -1, left.priority_compare(right), "#{priority(left)} is not less than #{priority(right)}" + assert_equal(-1, left.priority_compare(right), "#{priority(left)} is not less than #{priority(right)}") end def assert_priority_same(left, right) @@ -367,7 +367,6 @@ def assert_priority(left, middle, right) assert_priority text_1, text_1p, text_1b end - it "sorts (3) based on the registration state" do text_1.registered = text_1p.registered = true text_1b = mime_type(text_1) { |t| t.registered = false } @@ -382,7 +381,6 @@ def assert_priority(left, middle, right) assert_priority text_1, text_1p, text_1b end - it "sorts (5) based on the use-instead value" do text_1.obsolete = text_1p.obsolete = true text_1.use_instead = text_1p.use_instead = "abc/xyz" @@ -401,7 +399,6 @@ def assert_priority(left, middle, right) assert_priority_less text_1, text_2 end - end describe "#raw_media_type" do diff --git a/test/test_mime_types_class.rb b/test/test_mime_types_class.rb index b42d643..66122f3 100644 --- a/test/test_mime_types_class.rb +++ b/test/test_mime_types_class.rb @@ -86,7 +86,7 @@ def setup assert_equal %w[image/jpeg], MIME::Types.of(["foo.jpeg", "bar.jpeg"]) end - it "finds multiple extensions" do + it "finds multiple extensions ordered by the filename list" do assert_equal %w[text/plain image/jpeg], MIME::Types.type_for(%w[foo.txt foo.jpeg]) end