Class: OptParseValidator::OptPath
- Inherits:
-
OptBase
- Object
- OptBase
- OptParseValidator::OptPath
show all
- Defined in:
- lib/opt_parse_validator/opts/path.rb
Overview
Implementation of the Path Option
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
Instance Method Details
#allowed_attrs ⇒ Object
32
33
34
|
# File 'lib/opt_parse_validator/opts/path.rb', line 32
def allowed_attrs
i[create file directory executable readable writable]
end
|
#check_directory(path) ⇒ Object
44
45
46
|
# File 'lib/opt_parse_validator/opts/path.rb', line 44
def check_directory(path)
raise Error, "'#{path}' is not a directory" unless path.directory? || attrs[:exists] == false
end
|
#check_executable(path) ⇒ Object
49
50
51
|
# File 'lib/opt_parse_validator/opts/path.rb', line 49
def check_executable(path)
raise Error, "'#{path}' is not executable" unless path.executable?
end
|
#check_file(path) ⇒ Object
39
40
41
|
# File 'lib/opt_parse_validator/opts/path.rb', line 39
def check_file(path)
raise Error, "'#{path}' is not a file" unless path.file? || attrs[:exists] == false
end
|
#check_readable(path) ⇒ Object
54
55
56
|
# File 'lib/opt_parse_validator/opts/path.rb', line 54
def check_readable(path)
raise Error, "'#{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
62
63
64
|
# File 'lib/opt_parse_validator/opts/path.rb', line 62
def check_writable(path)
raise Error, "'#{path}' is not writable" if path.exist? && !path.writable? || !path.parent.writable?
end
|
#validate(value) ⇒ String
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/opt_parse_validator/opts/path.rb', line 21
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
|