lib/magic.rb in magic-0.1.1 vs lib/magic.rb in magic-0.2.0
- old
+ new
@@ -11,59 +11,61 @@
class << self
# Guesses mime of given file
# ====== Example
# Magic.guess_file_mime("public/images/rails.png")
# # => "image/png; charset=binary"
- def guess_file_mime(file)
- guess(:file, :mime, file)
+ def guess_file_mime(filename, *args)
+ guess(*args.unshift(:mime)) { |db| db.file(filename) }
end
# Guesses mime encoding of given file
# ===== Example
# Magic.guess_file_mime_encoding("public/images/rails.png")
# # => "binary"
- def guess_file_mime_encoding(file)
- guess(:file, :mime_encoding, file)
+ def guess_file_mime_encoding(filename, *args)
+ guess(*args.unshift(:mime_encoding)) { |db| db.file(filename) }
end
# Guesses mime type of given file
# ===== Example
# Magic.guess_file_mime_type("public/images/rails.png")
# # => "image/png"
- def guess_file_mime_type(file)
- guess(:file, :mime_type, file)
+ def guess_file_mime_type(filename, *args)
+ guess(*args.unshift(:mime_type)) { |db| db.file(filename) }
end
# Guesses mime type of given string
# ===== Example
# Magic.guess_string_mime("Magic® File™")
# # => "text/plain; charset=utf-8"
- def guess_string_mime(string)
- guess(:buffer, :mime, string)
+ def guess_string_mime(string, *args)
+ guess(*args.unshift(:mime)) { |db| db.buffer(string) }
end
# Guesses mime type of given string
# ===== Example
# Magic.guess_string_mime_encoding("Magic® File™")
# # => "utf-8"
- def guess_string_mime_encoding(string)
- guess(:buffer, :mime_encoding, string)
+ def guess_string_mime_encoding(string, *args)
+ guess(*args.unshift(:mime_type)) { |db| db.buffer(string) }
end
# Guesses mime type of given string
# ===== Example
# Magic.guess_string_mime_type("Magic® File™")
# # => "text/plain"
- def guess_string_mime_type(string)
- guess(:buffer, :mime_type, string)
+ def guess_string_mime_type(string, *args)
+ guess(*args.unshift(:mime_type)) { |db| db.buffer(string) }
end
- protected
-
- def guess(type, what, where) #:nodoc:
- db = Database.new(what)
- result = db.send(type, where)
- db.close
- result
+ # Creates magic database and yields it to the given block
+ # ===== Example
+ # Magic.guess(:mime) { |db| db.buffer("Magic® File™") }
+ # # => "text/plain; charset=utf-8"
+ def guess(*args)
+ db = Database.new(*args)
+ yield(db).tap do
+ db.close
+ end
end
end
end