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 Oct 2, 2024
1 parent 0d6d589 commit 194dc97
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/mime/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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 @@ -705,15 +705,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
7 changes: 2 additions & 5 deletions test/test_mime_type.rb
Original file line number Diff line number Diff line change
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 194dc97

Please sign in to comment.