class OkComputer::DirectoryCheck

Check if a file system directory exists and has the correct access. This may prove useful if the application relies on a mounted shared file system.

Constants

ConnectionFailed

Attributes

directory[RW]
writable[RW]

Public Class Methods

new(directory, writable = true) click to toggle source

Public: Initialize a new directory check.

directory - the path of the directory. Can be relative or absolute. writable - true if directory should allow writes; false if not.

# File lib/ok_computer/built_in_checks/directory_check.rb, line 13
def initialize(directory, writable = true)
  raise ArgumentError if directory.blank?

  self.directory = directory
  self.writable = writable
end

Public Instance Methods

check() click to toggle source

Public: Return the status of the directory check

# File lib/ok_computer/built_in_checks/directory_check.rb, line 21
def check
  stat = File.stat(directory) if File.exist?(directory)
  if stat
    if stat.directory?
      if !stat.readable?
        mark_message "Directory '#{directory}' is not readable."
        mark_failure
      elsif writable && !stat.writable?
        mark_message "Directory '#{directory}' is not writable."
        mark_failure
      elsif !writable && stat.writable?
        mark_message "Directory '#{directory}' is writable (undesired)."
        mark_failure
      else
        mark_message "Directory '#{directory}' is #{writable ? nil : 'NOT '}writable (as expected)."
      end
    else
      mark_message "'#{directory}' is not a directory."
      mark_failure
    end
  else
    mark_message "Directory '#{directory}' does not exist."
    mark_failure
  end
end