Class: Magic
- Inherits:
-
Object
- Object
- Magic
- Extended by:
- FFI::Library
- Defined in:
- lib/magic.rb,
lib/magic/version.rb
Constant Summary collapse
- NONE =
0x000000- DEBUG =
0x000001- SYMLINK =
0x000002- COMPRESS =
0x000004- DEVICES =
0x000008- MIME_TYPE =
0x000010- CONTINUE =
0x000020- CHECK =
0x000040- PRESERVE_ATIME =
0x000080- RAW =
0x000100- ERROR =
0x000200- MIME_ENCODING =
0x000400- MIME =
MIME_TYPE | MIME_ENCODING
- APPLE =
0x000800- NO_CHECK_COMPRESS =
0x001000- NO_CHECK_TAR =
0x002000- NO_CHECK_SOFT =
0x004000- NO_CHECK_APPTYPE =
0x008000- NO_CHECK_ELF =
0x010000- NO_CHECK_TEXT =
0x020000- NO_CHECK_CDF =
0x040000- NO_CHECK_TOKENS =
0x100000- NO_CHECK_ENCODING =
0x200000- Error =
Class.new(StandardError)
- VERSION =
"1.0.0"
Instance Attribute Summary collapse
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #buffer(string) ⇒ Object
- #close ⇒ Object
- #file(path) ⇒ Object
-
#initialize(flags = NONE, path = nil) ⇒ Magic
constructor
A new instance of Magic.
- #load(path = @path) ⇒ Object
- #valid?(path = nil) ⇒ Boolean
Constructor Details
#initialize(flags = NONE, path = nil) ⇒ Magic
Returns a new instance of Magic.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/magic.rb', line 57 def initialize(flags = NONE, path = nil) @flags = flags @path = path unless = magic_open(@flags) raise Error, "failed to initialize magic cookie" end if magic_load(, @path) != 0 raise Error, "failed to load database: #{magic_error(@cookie)}" end begin ObjectSpace.define_finalizer(self){ close } rescue end end |
Instance Attribute Details
#flags ⇒ Object
Returns the value of attribute flags.
43 44 45 |
# File 'lib/magic.rb', line 43 def flags @flags end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
43 44 45 |
# File 'lib/magic.rb', line 43 def path @path end |
Class Method Details
.compile(path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/magic.rb', line 45 def self.compile(path) = magic_open(NONE) unless magic_compile(, path) == 0 raise Error, "failed compile: #{magic_error(cookie)}" end magic_close() true end |
Instance Method Details
#buffer(string) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/magic.rb', line 83 def buffer(string) input = string.to_str if input.respond_to?(:bytesize) magic_buffer(, input, input.bytesize) else magic_buffer(, input, input.size) end end |
#close ⇒ Object
109 110 111 112 |
# File 'lib/magic.rb', line 109 def close magic_close() if = nil end |
#file(path) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/magic.rb', line 75 def file(path) unless msg = magic_file(, path) raise Error, "failed lookup: #{magic_error(@cookie)}" end msg end |
#load(path = @path) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/magic.rb', line 102 def load(path = @path) unless magic_load(, path) == 0 raise Error, "failed load: #{magic_error(@cookie)}" end @path = path end |
#valid?(path = nil) ⇒ Boolean
98 99 100 |
# File 'lib/magic.rb', line 98 def valid?(path = nil) magic_check(, path) == 0 end |