Class: OkComputer::DirectoryCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/directory_check.rb

Overview

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.

Constant Summary collapse

ConnectionFailed =
Class.new(StandardError)

Constants inherited from Check

Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

Constructor Details

#initialize(directory, writable = true) ⇒ DirectoryCheck

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.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# 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

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



7
8
9
# File 'lib/ok_computer/built_in_checks/directory_check.rb', line 7

def directory
  @directory
end

#writableObject

Returns the value of attribute writable.



7
8
9
# File 'lib/ok_computer/built_in_checks/directory_check.rb', line 7

def writable
  @writable
end

Instance Method Details

#checkObject

Public: Return the status of the directory check



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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