Class: Magic::Database
- Inherits:
-
Object
- Object
- Magic::Database
- Defined in:
- lib/magic/database.rb
Instance Attribute Summary collapse
-
#flags ⇒ Object
Returns the value of attribute flags.
Instance Method Summary collapse
-
#buffer(string) ⇒ Object
Determine type of given string.
-
#close ⇒ Object
Closes the database.
-
#error ⇒ Object
Returns the last error occured.
-
#file(filename) ⇒ Object
Determine type of a file at given path.
-
#initialize(*args) ⇒ Database
constructor
Creates an instance of
Magic::Database
using given database file and flags. -
#load(database = nil) ⇒ Object
Loads given database file (or default if
nil
given). -
#open(*flags) ⇒ Object
Opens magic db using given flags.
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) = args.last.is_a?(Hash) ? args.pop : {} # extract options database = .delete(:database) open(*args) load(database) end |
Instance Attribute Details
#flags ⇒ Object
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 |
#close ⇒ Object
Closes the database
21 22 23 |
# File 'lib/magic/database.rb', line 21 def close Api.magic_close(@magic_set) end |
#error ⇒ Object
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
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 |