class OptParseValidator::OptFilePath

Implementation of the FilePath Option

Public Class Methods

new(option, attrs = {}) click to toggle source

@param [ Array ] option See OptBase#new @param [ Hash ] attrs See OptPath#new :extensions [ Array | String ] The allowed extension(s)

Calls superclass method
# File lib/opt_parse_validator/opts/file_path.rb, line 9
def initialize(option, attrs = {})
  super(option, attrs)

  @attrs.merge!(file: true)
end

Public Instance Methods

allowed_attrs() click to toggle source
Calls superclass method
# 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) click to toggle source

@param [ Pathname ] path

# 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) click to toggle source
# 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