class Magic::Database
Attributes
flags[R]
Public Class Methods
new(*args)
click to toggle source
Creates an instance of +Magic::Database+ using given database file and flags
# File lib/magic/database.rb, line 7 def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} # extract options database = options.delete(:database) open(*args) load(database) end
Public Instance Methods
buffer(string)
click to toggle source
Determine type of given string
# File lib/magic/database.rb, line 42 def buffer(string) result = Api.magic_buffer(@magic_set, string, string.bytesize) if result.null? raise Error, error else result.get_string(0) end end
close()
click to toggle source
Closes the database
# File lib/magic/database.rb, line 21 def close Api.magic_close(@magic_set) end
error()
click to toggle source
Returns the last error occured
# File lib/magic/database.rb, line 52 def error Api.magic_error(@magic_set) end
file(filename)
click to toggle source
Determine type of a file at given path
# File lib/magic/database.rb, line 31 def file(filename) raise Errno::ENOENT, filename unless File.exists?(filename) result = Api.magic_file(@magic_set, filename.to_s) if result.null? raise Error, error else result.get_string(0) end end
flags=(*flags)
click to toggle source
Sets the flags
# File lib/magic/database.rb, line 57 def flags=(*flags) @flags = calculate_flags(*flags) Api.magic_setflags(@magic_set, @flags) end
load(database = nil)
click to toggle source
Loads given database file (or default if nil
given)
# File lib/magic/database.rb, line 26 def load(database = nil) Api.magic_load(@magic_set, database) end
open(*flags)
click to toggle source
Opens magic db using given flags
# File lib/magic/database.rb, line 15 def open(*flags) @flags = calculate_flags(*flags) @magic_set = Api.magic_open(@flags) end
Protected Instance Methods
calculate_flags(*flags)
click to toggle source
# File lib/magic/database.rb, line 64 def calculate_flags(*flags) flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) } end