module File_Checking
A module to facilitate frequently occuring checks on file-system objects
Public Class Methods
Checks if the file with the name from the first parameter has the properties, listed in the second. The messages parameter is an array of one or all of :exist?, :readable?, :writable?, :directory? or their string-representations, respectively. Returns nil in case of success, otherwise an informative message, describing the first negative test-result.
# File lib/file_checking.rb, line 70 def self.file_check(file, *messages) msg = nil if(file && messages.respond_to?(:to_ary) && !messages.empty?) messages.each do |k| if(! k.to_s.end_with?('?')) k = (k.to_s << '?').to_sym end @log.debug ('checking ' << k.to_s) if @log if(msg == nil && File.respond_to?(k) && ! File.send(k, file.to_s)) msg = "#{file} #{@@text_messages[k.to_sym]}" end end end msg end
# File lib/file_checking.rb, line 95 def self.magic_check(file, magic) fm = FileMagic.fm fd = fm.fd(File.new(file) ).split(';')[0] if fd != magic.strip return file.dup << ' ' << (@@text_messages[:type] %[magic, fd]) end return nil end
# File lib/file_checking.rb, line 86 def self.mime_check(file, mime_type) fm = FileMagic.mime fd = fm.fd(File.new(file) ).split(';')[0] if fd != mime_type.strip return file.dup << ' ' << (@@text_messages[:mime] %[mime_type, fd]) end return nil end
Public Instance Methods
Checks if the file with the name from the first parameter has the properties, listed in the second. The messages parameter is an array of one or several of :exist?, :readable?, :writable?, :directory? or their string-representations, respectively. Returns nil in case of success, otherwise an informative message, describing the first negative test-result.
# File lib/file_checking.rb, line 58 def file_check(file, *messages) File_Checking.file_check(file, *messages) end