Class: OptParseValidator::OptFilePath

Inherits:
OptPath show all
Defined in:
lib/opt_parse_validator/opts/file_path.rb

Overview

Implementation of the FilePath Option

Instance Attribute Summary

Attributes inherited from OptBase

#attrs, #option, #required

Instance Method Summary collapse

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)

Parameters:

  • option (Array)

    See OptBase#new

  • attrs (Hash) (defaults to: {})

    See OptPath#new



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_attrsObject



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

Parameters:

  • path (Pathname)


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

Raises:



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