Class: FilePipeline::Versions::Validator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/file_pipeline/versions/validator.rb

Overview

Validator objects verify the version file and results returned by a FileOperation.

They will validate:

  • that the version file existst

  • that it is in the correct directory

  • that the file operation has not returned any failures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_info, directory, filename) ⇒ Validator

Returns a new instance.

Arguments
  • version_info - path to an existing file or an array with the path and optionally a FileOperations::Results instance.

  • directory - directory where the file is expected (the working directory of a VersionedFile).

  • filename - name of the file to be returned if the file operation was was non-modifying (usually the VersionedFile#original).



31
32
33
34
35
# File 'lib/file_pipeline/versions/validator.rb', line 31

def initialize(version_info, directory, filename)
  @file, @info = [version_info].flatten
  @directory = directory
  @filename = filename
end

Instance Attribute Details

#fileObject (readonly)

File for the version that resulted from a FileOperation.



16
17
18
# File 'lib/file_pipeline/versions/validator.rb', line 16

def file
  @file
end

#infoObject (readonly)

FileOperation::Results object.



19
20
21
# File 'lib/file_pipeline/versions/validator.rb', line 19

def info
  @info
end

Class Method Details

.[](version_info, versioned_file) ⇒ Object

Validates file, directory, and info for version_info in the context of versioned_file.

Arguments
  • version_info - path to an existing file or an array with the path and optionally a FileOperations::Results instance.

  • versioned_file - an object that responds to #original and

returns a file path, and #directory and returns a directory path.



46
47
48
49
50
51
52
# File 'lib/file_pipeline/versions/validator.rb', line 46

def self.[](version_info, versioned_file)
  new(version_info, versioned_file.directory, versioned_file.original)
    .validate_info
    .validate_file
    .validate_directory
    .then { |validator| [validator.file, validator.info] }
end

Instance Method Details

#unmodified?Boolean

Returns true when there is no file for the version (result of a non-modifying file operation), false otherwise.



56
57
58
# File 'lib/file_pipeline/versions/validator.rb', line 56

def unmodified?
  @file.nil?
end

#validate_directoryObject

Raises MisplacedVersionFileError if #file is not in #directory.



61
62
63
64
65
66
# File 'lib/file_pipeline/versions/validator.rb', line 61

def validate_directory
  return self if unmodified? || File.dirname(@file) == @directory

  raise Errors::MisplacedVersionFileError.new file: @file,
                                              directory: @directory
end

#validate_fileObject

Raises MissingVersionFileError if #file does not exist on the file system.



70
71
72
73
74
# File 'lib/file_pipeline/versions/validator.rb', line 70

def validate_file
  return self if unmodified? || File.exist?(@file)

  raise Errors::MissingVersionFileError.new file: @file
end

#validate_infoObject

Raises FailedModificationError if the file operation generatint the #info failed.



78
79
80
81
82
# File 'lib/file_pipeline/versions/validator.rb', line 78

def validate_info
  return self unless @info&.failure

  raise Errors::FailedModificationError.new info: @info, file: @filename
end