Skip to content

Commit

Permalink
More WIP, still does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
halostatue committed Sep 10, 2024
1 parent 6ecf029 commit c17b330
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
12 changes: 6 additions & 6 deletions lib/mime/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions lib/mime/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/mime/types/deprecations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 7 additions & 10 deletions test/test_mime_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand All @@ -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"
Expand All @@ -401,7 +399,6 @@ def assert_priority(left, middle, right)

assert_priority_less text_1, text_2
end

end

describe "#raw_media_type" do
Expand Down
2 changes: 1 addition & 1 deletion test/test_mime_types_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit c17b330

Please sign in to comment.