Sha256: ef68a09a4d58f330b9bb8c3ad45bd06ad46e9ff5d9b3de0d26c3f8bb07c60fbc

Contents?: true

Size: 897 Bytes

Versions: 9

Compression:

Stored size: 897 Bytes

Contents

# frozen_string_literal: true

module OptParseValidator
  # Implementation of the FilePath Option
  class OptFilePath < OptPath
    # @param [ Array ] option See OptBase#new
    # @param [ Hash ] attrs See OptPath#new
    # :extensions [ Array | String ] The allowed extension(s)
    def initialize(option, attrs = {})
      super(option, attrs)

      @attrs.merge!(file: true)
    end

    # @param [ Pathname ] path
    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

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

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

      raise Error, "The extension of '#{path}' is not allowed"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
opt_parse_validator-1.9.1 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.9.0 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.8.2 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.8.1 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.8.0 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.7.4 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.7.3 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-1.7.2 lib/opt_parse_validator/opts/file_path.rb
opt_parse_validator-0.0.17.1 lib/opt_parse_validator/opts/file_path.rb