Class Magic::Database
In: lib/magic/database.rb
Parent: Object

Methods

buffer   calculate_flags   close   error   file   flags=   load   new   open  

Attributes

flags  [R] 

Public Class methods

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

[Source]

# 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

Determine type of given string

[Source]

# File lib/magic/database.rb, line 41
    def buffer(string)
      result = Api.magic_buffer(@magic_set, string, string.bytesize)
      if result.null?
        raise Exception, error
      else
        result.get_string(0)
      end
    end

Closes the database

[Source]

# File lib/magic/database.rb, line 21
    def close
      Api.magic_close(@magic_set)
    end

Returns the last error occured

[Source]

# File lib/magic/database.rb, line 51
    def error
      Api.magic_error(@magic_set)
    end

Determine type of a file at given path

[Source]

# File lib/magic/database.rb, line 31
    def file(filename)
      result = Api.magic_file(@magic_set, filename.to_s)
      if result.null?
        raise Exception, error
      else
        result.get_string(0)
      end
    end

Sets the flags

[Source]

# File lib/magic/database.rb, line 56
    def flags=(*flags)
      @flags = calculate_flags(*flags)
      Api.magic_setflags(@magic_set, @flags)
    end

Loads given database file (or default if nil given)

[Source]

# File lib/magic/database.rb, line 26
    def load(database = nil)
      Api.magic_load(@magic_set, database)
    end

Opens magic db using given flags

[Source]

# File lib/magic/database.rb, line 15
    def open(*flags)
      @flags = calculate_flags(*flags)
      @magic_set = Api.magic_open(@flags)
    end

Protected Instance methods

[Source]

# File lib/magic/database.rb, line 63
    def calculate_flags(*flags)
      flags.inject(0) { |calculated, flag| calculated |= Constants::Flag.const_get(flag.to_s.upcase) }
    end

[Validate]