Class: Magic

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

Overview

:startdoc:

Constant Summary collapse

VERSION =

Current version of Magic.

'0.2.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.check(path) ⇒ Object

call-seq:

Magic.check( path, ... ) -> true or false
Magic.check( array )     -> true or false

Returns

Example:

Will raise

See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile



191
192
193
# File 'lib/magic.rb', line 191

def check(path)
  open {|m| m.check(path) }
end

.compile(path) ⇒ Object

call-seq:

Magic.compile( path, ... ) -> true
Magic.compile( array )     -> true

Returns

Example:

Will raise

See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check



174
175
176
# File 'lib/magic.rb', line 174

def compile(path)
  open {|m| m.compile(path) }
end

.encoding(&block) ⇒ Object

call-seq:

Magic.encoding                  -> self
Magic.encoding {|magic| block } -> string or array

Returns

Example:

Will raise

See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check



157
158
159
# File 'lib/magic.rb', line 157

def encoding(&block)
  open(Magic::MIME_ENCODING, &block)
end

.mime(&block) ⇒ Object

call-seq:

Magic.mime                  -> self
Magic.mime {|magic| block } -> string or array

Returns

Example:

Will raise

See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check



123
124
125
# File 'lib/magic.rb', line 123

def mime(&block)
  open(Magic::MIME, &block)
end

.open(flags = Magic::NONE) ⇒ Object

call-seq:

Magic.open( flags )                  -> self
Magic.open( flags ) {|magic| block } -> string or array

Returns

Example:

Will raise

See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/magic.rb', line 95

def open(flags = Magic::NONE)
  magic = Magic.new
  magic.flags = flags

  if block_given?
    begin
      yield magic
    ensure
      magic.close
    end
  else
    magic
  end
end

.type(&block) ⇒ Object

call-seq:

Magic.type                  -> self
Magic.type {|magic| block } -> string or array

Returns

Example:

Will raise

See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check



140
141
142
# File 'lib/magic.rb', line 140

def type(&block)
  open(Magic::MIME_TYPE, &block)
end

.version_to_aObject Also known as: version_array

call-seq:

Magic.version_to_a -> array

Returns

Example:

Magic.version_to_a   #=> [5, 17]

Will raise Magic::NotImplementedError exception if, or

See also: Magic::version_to_s



46
47
48
# File 'lib/magic/version.rb', line 46

def version_to_a
  [self.version / 100, self.version % 100]
end

.version_to_sObject Also known as: version_string

call-seq:

Magic.version_to_s -> string

Returns

Example:

Magic.version_to_s   #=> "5.17"

Will raise Magic::NotImplementedError exception if, or

See also: Magic::version_to_a



64
65
66
# File 'lib/magic/version.rb', line 64

def version_to_s
  '%d.%02d' % self.version_to_a
end

Instance Method Details

#flags_to_a(names = false) ⇒ Object Also known as: flags_array

call-seq:

magic.flags_to_a( names ) -> array

Returns an array

Example:

Will raise Magic::LibraryError exception if, or

See also: Magic#flags

Raises:

  • (LibraryError)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/magic.rb', line 63

def flags_to_a(names = false)
  raise LibraryError, 'Magic library is not open' if closed?
  return [names ? 'NONE' : 0] if @flags.zero?

  n, i = 0, @flags
  flags = []

  @@flags_map ||= flags_as_map if names

  while i > 0
    n = 2 ** (Math.log(i) / Math.log(2)).to_i
    i = i - n
    flags.insert(0, names ? @@flags_map[n] : n)
  end

  flags
end

#inspectObject

call-seq:

magic.inspect -> string

Returns

Example:

magic = Magic.new   #=> #<Magic:0x007fd5258a1108>
magic.inspect       #=> "#<Magic:0x007fd5258a1108 @flags=0, @path=[\"/etc/magic\", \"/usr/share/misc/magic\"]>"


47
48
49
# File 'lib/magic.rb', line 47

def inspect
  super.insert(-2, self.closed? ? ' (closed)' : '')
end