Skip to content

Commit e4896ee

Browse files
authored
Merge pull request #72 from casperisfine/strip-empty-arrays
Store MIME parents in a distinct Hash
2 parents 9ee8f63 + e413dae commit e4896ee

File tree

5 files changed

+1169
-909
lines changed

5 files changed

+1169
-909
lines changed

lib/marcel/magic.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ def initialize(type)
2929
# * <i>:comment</i>: Comment string
3030
def self.add(type, options)
3131
extensions = [options[:extensions]].flatten.compact
32-
TYPES[type] = [extensions,
33-
[options[:parents]].flatten.compact,
34-
options[:comment]]
32+
TYPE_EXTS[type] = extensions
33+
parents = [options[:parents]].flatten.compact
34+
TYPE_PARENTS[type] = parents unless parents.empty?
3535
extensions.each {|ext| EXTENSIONS[ext] = type }
3636
MAGIC.unshift [type, options[:magic]] if options[:magic]
3737
end
@@ -42,7 +42,8 @@ def self.add(type, options)
4242
def self.remove(type)
4343
EXTENSIONS.delete_if {|ext, t| t == type }
4444
MAGIC.delete_if {|t, m| t == type }
45-
TYPES.delete(type)
45+
TYPE_EXTS.delete(type)
46+
TYPE_PARENTS.delete(type)
4647
end
4748

4849
# Returns true if type is a text format
@@ -60,12 +61,12 @@ def child_of?(parent)
6061

6162
# Get string list of file extensions
6263
def extensions
63-
TYPES.key?(type) ? TYPES[type][0] : []
64+
TYPE_EXTS[type] || []
6465
end
6566

6667
# Get mime comment
6768
def comment
68-
(TYPES.key?(type) ? TYPES[type][2] : nil).to_s
69+
nil # deprecated
6970
end
7071

7172
# Lookup mime type by file extension
@@ -110,7 +111,7 @@ def hash
110111
alias == eql?
111112

112113
def self.child?(child, parent)
113-
child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
114+
child == parent || TYPE_PARENTS[child]&.any? {|p| child?(p, parent) }
114115
end
115116

116117
def self.magic_match(io, method)

lib/marcel/mime_type.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ class MimeType
66

77
class << self
88
def extend(type, extensions: [], parents: [], magic: nil)
9-
existing = Marcel::TYPES[type] || [[], [], ""]
10-
11-
extensions = (Array(extensions) + existing[0]).uniq
12-
parents = (Array(parents) + existing[1]).uniq
13-
comment = existing[2]
14-
15-
Magic.add(type, extensions: extensions, magic: magic, parents: parents, comment: comment)
9+
extensions = (Array(extensions) + Array(Marcel::TYPE_EXTS[type])).uniq
10+
parents = (Array(parents) + Array(Marcel::TYPE_PARENTS[type])).uniq
11+
Magic.add(type, extensions: extensions, magic: magic, parents: parents)
1612
end
1713

1814
# Returns the most appropriate content type for the given file.
@@ -42,6 +38,7 @@ def for(pathname_or_io = nil, name: nil, extension: nil, declared_type: nil)
4238
end
4339

4440
private
41+
4542
def for_data(pathname_or_io)
4643
if pathname_or_io
4744
with_io(pathname_or_io) do |io|
@@ -103,10 +100,10 @@ def most_specific_type(from_magic_type, fallback_type)
103100
end
104101

105102
def root_types(type)
106-
if TYPES[type].nil? || TYPES[type][1].empty?
103+
if TYPE_EXTS[type].nil? || TYPE_PARENTS[type].nil?
107104
[ type ]
108105
else
109-
TYPES[type][1].map {|t| root_types t }.flatten
106+
TYPE_PARENTS[type].map {|t| root_types t }.flatten
110107
end
111108
end
112109
end

0 commit comments

Comments
 (0)