module FilePipeline

Module that contains classes to build a file processing pipeline that applies a defined batch of file operations non-destructively to a VersionedFile.

Constants

DEFAULT_DIR

Constant for the defualt directory for file operations.

Public Class Methods

<<(directory) click to toggle source

Adds directory to the Array of source_directories which will be searched for source files when loading file operations. directory will be prepended. Therefore, directories will be searcherd in reverse order of them being added.

# File lib/file_pipeline.rb, line 21
def self.<<(directory)
  directory_path = File.expand_path directory
  return source_directories if source_directories.include? directory_path

  no_dir = !File.directory?(directory_path)
  raise Errors::SourceDirectoryError.new dir: directory if no_dir

  @src_directories.prepend directory_path
end
load(file_operation) click to toggle source

Returns the constant for the file_operation class. If the constant is not defined, will try to require the source file.

# File lib/file_pipeline.rb, line 33
def self.load(file_operation)
  const = file_operation.split('_').map(&:capitalize).join
  FilePipeline.load_file(file_operation) unless const_defined? const
  const_get "FileOperations::#{const}"
rescue NameError
  # TODO: implement autogenerating module names from file_operation src path
  const_get const
end
load_file(src_file) click to toggle source

Will search for src_file in .source_directories and require the file if.

# File lib/file_pipeline.rb, line 44
def self.load_file(src_file)
  src_file += '.rb' unless src_file.end_with? '.rb'
  src_path = FilePipeline.source_path src_file
  if src_path.nil?
    raise Errors::SourceFileError.new(
      file: src_file,
      directories: FilePipeline.source_directories
    )
  end
  require src_path
end
new_basename(kind = :timestamp) click to toggle source

Creates a file basename consisting of either a timestamp or a UUID, depending on the kind argument (:timestamp or :random; default: :timestamp)

# File lib/file_pipeline.rb, line 59
def self.new_basename(kind = :timestamp)
  case kind
  when :random
    SecureRandom.uuid
  when :timestamp
    Time.now.strftime('%Y-%m-%dT%H:%M:%S.%N')
  end
end
path(dir, filename) click to toggle source

Returns a String with the /directory/filename.

# File lib/file_pipeline.rb, line 69
def self.path(dir, filename)
  File.join dir, filename
end
source_directories() click to toggle source

Returns an array of directory paths that may contain source files for file operation classes.

# File lib/file_pipeline.rb, line 75
def self.source_directories
  return @src_directories if @src_directories

  @src_directories = [FilePipeline.path(__dir__, DEFAULT_DIR)]
end
source_path(file) click to toggle source

Searches .source_directories and for file, and returns the full path (directory and filename) for the first match or nil if the file is nowhere found. Since directories are added in reverse order (see .<<) this will give redefinitions of file operations in custom directories precedence over the default directory, thus allowing overriding of file operation definitions.

# File lib/file_pipeline.rb, line 87
def self.source_path(file)
  FilePipeline.source_directories.each do |dir|
    full_path = FilePipeline.path(dir, file)
    return full_path if File.exist? full_path
  end
  nil
end