Module: FileDescription

Included in:
File
Defined in:
lib/filedesc.rb

Constant Summary collapse

OPTIONS =
{:executable => '/usr/bin/file' }

Instance Method Summary collapse

Instance Method Details

#descriptionObject



19
20
21
22
# File 'lib/filedesc.rb', line 19

def description
  @description ||= get_output
  @description
end

#extensionObject



28
29
30
# File 'lib/filedesc.rb', line 28

def extension
  self.path.scan(/\.([A-za-z0-9]+)$/).flatten.last.to_s
end

#has_description?(regexp) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/filedesc.rb', line 24

def has_description?(regexp)
  !(description !~ regexp)
end

#infoObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/filedesc.rb', line 36

def info
    {
      :filename =>   File.basename(self.path),
      :mime =>   mime,
      :description =>   description,
      :extension =>   extension,
      :directory =>  File.dirname(self.path),
      :path =>  self.path,
      :size => File.size(self.path),
      :last_modified => File.mtime(self.path),
      :last_access => File.atime(self.path),
      :executable => File.executable?(self.path),
      :gid => File.stat(self.path).gid,
      :uid => File.stat(self.path).uid,
      :mode => File.stat(self.path).mode

    }
end

#inspectObject



32
33
34
# File 'lib/filedesc.rb', line 32

def inspect
  "#<File:#{self.path}, mime: '#{mime}', description: '#{description}', extension: '#{extension}' >"
end

#is_a_mime?(type) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
# File 'lib/filedesc.rb', line 10

def is_a_mime?(type)
  return case type
  when String
    mime == type
  when Regexp
    !(mime !~ type)
  end
end

#mimeObject



5
6
7
8
# File 'lib/filedesc.rb', line 5

def mime
  @mime ||= get_output('-I')
  @mime
end