class OptParseValidator::OptPath

Implementation of the Path Option

Public Instance Methods

allowed_attrs() click to toggle source
# File lib/opt_parse_validator/opts/path.rb, line 34
def allowed_attrs
  %i[create file directory executable readable writable]
end
check_directory(path) click to toggle source

@param [ Pathname ] path

# File lib/opt_parse_validator/opts/path.rb, line 46
def check_directory(path)
  raise Error, "The path '#{path}' does not exist or is not a directory" unless path.directory? || attrs[:exists] == false
end
check_executable(path) click to toggle source

@param [ Pathname ] path

# File lib/opt_parse_validator/opts/path.rb, line 51
def check_executable(path)
  raise Error, "The path '#{path}' is not executable" unless path.executable?
end
check_file(path) click to toggle source

@param [ Pathname ] path

# File lib/opt_parse_validator/opts/path.rb, line 41
def check_file(path)
  raise Error, "The path '#{path}' does not exist or is not a file" unless path.file? || attrs[:exists] == false
end
check_readable(path) click to toggle source

@param [ Pathname ] path

# File lib/opt_parse_validator/opts/path.rb, line 56
def check_readable(path)
  raise Error, "The path '#{path}' is not readable" unless path.readable?
end
check_writable(path) click to toggle source

If the path does not exist, it will check for the parent directory write permission

@param [ Pathname ] path

# File lib/opt_parse_validator/opts/path.rb, line 64
def check_writable(path)
  raise Error, "The path '#{path}' is not writable" if (path.exist? && !path.writable?) || !path.parent.writable?
end
validate(value) click to toggle source

@param [ String ] value

@return [ String ]

# File lib/opt_parse_validator/opts/path.rb, line 23
def validate(value)
  path = Pathname.new(value)

  allowed_attrs.each do |key|
    method = "check_#{key}"
    send(method, path) if respond_to?(method) && attrs[key]
  end

  path.to_s
end