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

#choices, #default, #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



7
8
9
10
11
# File 'lib/opt_parse_validator/opts/file_path.rb', line 7

def initialize(option, attrs = {})
  super(option, attrs)

  @attrs.merge!(file: true)
end

Instance Method Details

#allowed_attrsObject



13
14
15
16
# File 'lib/opt_parse_validator/opts/file_path.rb', line 13

def allowed_attrs
  # :extensions is put at the first place
  [:extensions] + super
end

#check_extensions(path) ⇒ Object



18
19
20
21
22
# File 'lib/opt_parse_validator/opts/file_path.rb', line 18

def check_extensions(path)
  return if [*attrs[:extensions]].include?(path.extname.delete('.'))

  fail "The extension of '#{path}' is not allowed"
end