Class: Magic::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/magic/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Database

Creates an instance of Magic::Database using given database file and flags



7
8
9
10
11
12
# 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

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/magic/database.rb', line 3

def flags
  @flags
end

Instance Method Details

#buffer(string) ⇒ Object

Determine type of given string



42
43
44
45
46
47
48
49
# 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

#closeObject

Closes the database



21
22
23
# File 'lib/magic/database.rb', line 21

def close
  Api.magic_close(@magic_set)
end

#errorObject

Returns the last error occured



52
53
54
# File 'lib/magic/database.rb', line 52

def error
  Api.magic_error(@magic_set)
end

#file(filename) ⇒ Object

Determine type of a file at given path

Raises:

  • (Errno::ENOENT)


31
32
33
34
35
36
37
38
39
# 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

#load(database = nil) ⇒ Object

Loads given database file (or default if nil given)



26
27
28
# File 'lib/magic/database.rb', line 26

def load(database = nil)
  Api.magic_load(@magic_set, database)
end

#open(*flags) ⇒ Object

Opens magic db using given flags



15
16
17
18
# File 'lib/magic/database.rb', line 15

def open(*flags)
  @flags = calculate_flags(*flags)
  @magic_set = Api.magic_open(@flags)
end