lib/marcel/magic.rb in marcel-1.0.2 vs lib/marcel/magic.rb in marcel-1.0.3
- old
+ new
@@ -27,24 +27,25 @@
# * <i>:parents</i>: String list or single string of parent mime types
# * <i>:magic</i>: Mime magic specification
# * <i>:comment</i>: Comment string
def self.add(type, options)
extensions = [options[:extensions]].flatten.compact
- TYPES[type] = [extensions,
- [options[:parents]].flatten.compact,
- options[:comment]]
+ TYPE_EXTS[type] = extensions
+ parents = [options[:parents]].flatten.compact
+ TYPE_PARENTS[type] = parents unless parents.empty?
extensions.each {|ext| EXTENSIONS[ext] = type }
MAGIC.unshift [type, options[:magic]] if options[:magic]
end
# Removes a mime type from the dictionary. You might want to do this if
# you're seeing impossible conflicts (for instance, application/x-gmc-link).
# * <i>type</i>: The mime type to remove. All associated extensions and magic are removed too.
def self.remove(type)
EXTENSIONS.delete_if {|ext, t| t == type }
MAGIC.delete_if {|t, m| t == type }
- TYPES.delete(type)
+ TYPE_EXTS.delete(type)
+ TYPE_PARENTS.delete(type)
end
# Returns true if type is a text format
def text?; mediatype == 'text' || child_of?('text/plain'); end
@@ -58,16 +59,16 @@
self.class.child?(type, parent)
end
# Get string list of file extensions
def extensions
- TYPES.key?(type) ? TYPES[type][0] : []
+ TYPE_EXTS[type] || []
end
# Get mime comment
def comment
- (TYPES.key?(type) ? TYPES[type][2] : nil).to_s
+ nil # deprecated
end
# Lookup mime type by file extension
def self.by_extension(ext)
ext = ext.to_s.downcase
@@ -108,10 +109,10 @@
end
alias == eql?
def self.child?(child, parent)
- child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
+ child == parent || TYPE_PARENTS[child]&.any? {|p| child?(p, parent) }
end
def self.magic_match(io, method)
return magic_match(StringIO.new(io.to_s), method) unless io.respond_to?(:read)