class FilePipeline::Pipeline

Instances of Pipeline hold a defined set of operations that perform modifications of files.

The operations are applied to a VersionedFile in the order they are added to the instance. To implement custom operations, it is easiest to write a subclass of FileOperations::FileOperation.

The class can be initialized with an optional block to add file operations:

Pipeline.new do |pipeline|
  pipeline.define_operation('scale',
                            :width => 1280, :height => 1024)
  pipeline.define_operation('ptiff_conversion',
                            :tile_width => 64, :tile_height => 64)
end

Attributes

file_operations[R]

An array of file operations that will be applied to files in the order they have been added.

Public Class Methods

new(*src_directories) { |self| ... } click to toggle source

Returns a new instance.

If src_directories are provided, they will be added to FilePipeline.source_directories.

Arguments
  • src_directories - one or more paths to directories where classes for file operations are defined.

# File lib/file_pipeline/pipeline.rb, line 35
def initialize(*src_directories)
  src_directories.each { |dir| FilePipeline << dir }
  @file_operations = []
  yield(self) if block_given?
end

Public Instance Methods

<<(file_operation_instance) click to toggle source

Adds a file operation object file_operations. The object must implement a run method (see FileOperations::FileOperation#run for details).

# File lib/file_pipeline/pipeline.rb, line 43
def <<(file_operation_instance)
  validate file_operation_instance
  @file_operations << file_operation_instance
end
apply_to(versioned_file) click to toggle source

Applies all file_operations to a versioned_file and returns it.

# File lib/file_pipeline/pipeline.rb, line 49
def apply_to(versioned_file)
  file_operations.each { |job| run job, versioned_file }
  versioned_file
end
batch_apply(versioned_files) click to toggle source

Applies all file_operations to versioned_files (an array) and returns it.

# File lib/file_pipeline/pipeline.rb, line 56
def batch_apply(versioned_files)
  versioned_files.map { |file| Thread.new(file) { apply_to(file) } }
                 .map(&:value)
#--
# FIXME: This should obviously not just raise the same error again, but
#        rather do someting meaningful.
#++
rescue Errors::FailedModificationError => e
  raise e
end
define_operation(file_operation, options = {}) click to toggle source

Initializes the class for file_operation (a string in underscore notation) with options, adds it to file_operations, and returns self.

If the source file containing the file operation's class definition is not loaded, this method will try to locate it in the FilePipeline.source_directories and require it.

Examples

Define single operation:

pipeline.define_operation('ptiff_conversion', :tile => false)

Chaining:

pipeline.define_operation('scale', width: 1280, height: 1024)
        .define_operation('ptiff_conversion')
# File lib/file_pipeline/pipeline.rb, line 86
def define_operation(file_operation, options = {})
  operation = FilePipeline.load file_operation
  self << operation.new(**options)
  self
end
empty?() click to toggle source

Returns true if no file_operations are defined.

# File lib/file_pipeline/pipeline.rb, line 93
def empty?
  file_operations.empty?
end
run(operation, versioned_file) click to toggle source

Applies operation to versioned_file.

operation must be an object implementing a run method that takes three arguments (see FileOperations::FileOperation#run ).

# File lib/file_pipeline/pipeline.rb, line 101
def run(operation, versioned_file)
  versioned_file.modify do |version, directory, original|
    operation.run version, directory, original
  end
end

Private Instance Methods

validate(file_operation_instance) click to toggle source

Raises TypeError if file_operation_instance does not implement a run method.

# File lib/file_pipeline/pipeline.rb, line 111
def validate(file_operation_instance)
  return file_operation_instance if file_operation_instance.respond_to? :run

  raise TypeError, 'File operations must implement a #run method'
end