Class: OptParseValidator::OptFilePath
- Defined in:
- lib/opt_parse_validator/opts/file_path.rb
Overview
Implementation of the FilePath Option
Instance Attribute Summary
Attributes inherited from OptBase
Instance Method Summary collapse
- #allowed_attrs ⇒ Object
- #check_create(path) ⇒ Object
- #check_extensions(path) ⇒ Object
-
#initialize(option, attrs = {}) ⇒ OptFilePath
constructor
:extensions [ Array | String ] The allowed extension(s).
Methods inherited from OptPath
#check_directory, #check_executable, #check_file, #check_readable, #check_writable, #validate
Methods inherited from OptBase
#advanced?, #alias?, #append_help_messages, #choices, #default, #help_message_for_default, #help_messages, #normalize, #required?, #required_unless, #to_long, #to_s, #to_sym, #validate, #value_if_empty
Constructor Details
#initialize(option, attrs = {}) ⇒ OptFilePath
:extensions [ Array | String ] The allowed extension(s)
9 10 11 12 13 |
# File 'lib/opt_parse_validator/opts/file_path.rb', line 9 def initialize(option, attrs = {}) super(option, attrs) @attrs.merge!(file: true) end |
Instance Method Details
#allowed_attrs ⇒ Object
23 24 25 26 |
# File 'lib/opt_parse_validator/opts/file_path.rb', line 23 def allowed_attrs # :extensions is put at the first place [:extensions] + super end |
#check_create(path) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/opt_parse_validator/opts/file_path.rb', line 16 def check_create(path) return if File.exist?(path.to_s) FileUtils.mkdir_p(path.parent.to_s) unless Dir.exist?(path.parent.to_s) FileUtils.touch(path.to_s) end |
#check_extensions(path) ⇒ Object
28 29 30 31 32 |
# File 'lib/opt_parse_validator/opts/file_path.rb', line 28 def check_extensions(path) return if Array(attrs[:extensions]).include?(path.extname.delete('.')) raise Error, "The extension of '#{path}' is not allowed" end |