Class: FilePipeline::Versions::Validator
- Inherits:
-
Object
- Object
- FilePipeline::Versions::Validator
- 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
-
#file ⇒ Object
readonly
File for the version that resulted from a FileOperation.
-
#info ⇒ Object
readonly
FileOperation::Results object.
Class Method Summary collapse
-
.[](version_info, versioned_file) ⇒ Object
Validates file, directory, and info for
version_infoin the context ofversioned_file.
Instance Method Summary collapse
-
#initialize(version_info, directory, filename) ⇒ Validator
constructor
Returns a new instance.
-
#unmodified? ⇒ Boolean
Returns
truewhen there is no file for the version (result of a non-modifying file operation),falseotherwise. -
#validate_directory ⇒ Object
Raises MisplacedVersionFileError if #file is not in #directory.
-
#validate_file ⇒ Object
Raises MissingVersionFileError if #file does not exist on the file system.
-
#validate_info ⇒ Object
Raises FailedModificationError if the file operation generatint the #info failed.
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
#file ⇒ Object (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 |
#info ⇒ Object (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_directory ⇒ Object
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_file ⇒ Object
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_info ⇒ Object
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 |