Skip to content

Commit

Permalink
Resolve a bug related to the switch to Sets
Browse files Browse the repository at this point in the history
Fully resolves #117, #127, and #134.
  • Loading branch information
halostatue committed Aug 12, 2018
1 parent d5d7c57 commit edf3cd7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
10 changes: 9 additions & 1 deletion History.md
Expand Up @@ -11,10 +11,18 @@
true` to files so that modern Rubies can automatically reduce duplicate
string allocations. [#135][]

* 1 bug fix
* 2 bug fixes

* Burke Libbey fixed a problem with cached data loading. [#126][]

* Resolved an issue where Enumerable#inject returns +nil+ when provided
an empty enumerable and a default value has not been provided. This is
because when Enumerable#inject isn't provided a starting value, the
first value is used as the default value. In every case where this
error was happening, the result was supposed to be an array containing
Set objects so they can be reduced to a single Set. [#117][], [#127][],
[#134][].

* Deprecations:

* Lazy loading (`$RUBY_MIME_TYPES_LAZY_LOAD`) has been deprecated.
Expand Down
6 changes: 3 additions & 3 deletions lib/mime/types.rb
Expand Up @@ -151,7 +151,7 @@ def [](type_id, complete: false, registered: false)
def type_for(filename)
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
}.compact.inject(:+).sort { |a, b|
}.compact.inject(Set.new, :+).sort { |a, b|
a.priority_compare(b)
}
end
Expand All @@ -171,7 +171,7 @@ def add(*types)
nil
when MIME::Types
variants = mime_type.instance_variable_get(:@type_variants)
add(*variants.values.inject(:+).to_a, quiet)
add(*variants.values.inject(Set.new, :+).to_a, quiet)
when Array
add(*mime_type, quiet)
else
Expand Down Expand Up @@ -218,7 +218,7 @@ def prune_matches(matches, complete, registered)
def match(pattern)
@type_variants.select { |k, _|
k =~ pattern
}.values.inject(:+)
}.values.inject(Set.new, :+)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/mime/types/container.rb
Expand Up @@ -3,9 +3,9 @@
require 'set'

# MIME::Types requires a container Hash with a default values for keys
# resulting in an empty array (<tt>[]</tt>), but this cannot be dumped through
# Marshal because of the presence of that default Proc. This class exists
# solely to satisfy that need.
# resulting in an empty Set, but this cannot be dumped through Marshal because
# of the presence of that default Proc. This class exists solely to satisfy
# that need.
class MIME::Types::Container < Hash # :nodoc:
def initialize
super
Expand Down
6 changes: 6 additions & 0 deletions test/test_mime_types.rb
Expand Up @@ -85,6 +85,12 @@ def mime_types
refute_empty mime_types[/gzip/, registered: true]
refute_equal mime_types[/gzip/], mime_types[/gzip/, registered: true]
end

it 'properly returns an empty result on a regular expression miss' do
assert_empty mime_types[/^foo/]
assert_empty mime_types[/^foo/, registered: true]
assert_empty mime_types[/^foo/, complete: true]
end
end

describe '#add' do
Expand Down

0 comments on commit edf3cd7

Please sign in to comment.