Class: OptParseValidator::OptPath

Inherits:
OptBase
  • Object
show all
Defined in:
lib/opt_parse_validator/opts/path.rb

Overview

Implementation of the Path Option

Direct Known Subclasses

OptDirectoryPath, OptFilePath

Instance Attribute Summary

Attributes inherited from OptBase

#attrs, #option, #required

Instance Method Summary collapse

Methods inherited from OptBase

#advanced?, #alias?, #append_help_messages, #choices, #default, #help_message_for_default, #help_messages, #initialize, #normalize, #required?, #required_unless, #to_long, #to_s, #to_sym, #value_if_empty

Constructor Details

This class inherits a constructor from OptParseValidator::OptBase

Instance Method Details

#allowed_attrsObject



34
35
36
# File 'lib/opt_parse_validator/opts/path.rb', line 34

def allowed_attrs
  %i[create file directory executable readable writable]
end

#check_directory(path) ⇒ Object

Parameters:

  • path (Pathname)

Raises:



46
47
48
# 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) ⇒ Object

Parameters:

  • path (Pathname)

Raises:



51
52
53
# 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) ⇒ Object

Parameters:

  • path (Pathname)

Raises:



41
42
43
# 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) ⇒ Object

Parameters:

  • path (Pathname)

Raises:



56
57
58
# 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) ⇒ Object

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

Parameters:

  • path (Pathname)

Raises:



64
65
66
# 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) ⇒ String

Parameters:

  • value (String)

Returns:

  • (String)


23
24
25
26
27
28
29
30
31
32
# 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