Class: FilePipeline::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/file_pipeline/pipeline.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*src_directories) {|_self| ... } ⇒ Pipeline

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.

Yields:

  • (_self)

Yield Parameters:



35
36
37
38
39
# 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

Instance Attribute Details

#file_operationsObject (readonly)

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



24
25
26
# File 'lib/file_pipeline/pipeline.rb', line 24

def file_operations
  @file_operations
end

Instance Method Details

#<<(file_operation_instance) ⇒ Object

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



43
44
45
46
# 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) ⇒ Object

Applies all #file_operations to a versioned_file and returns it.



49
50
51
52
# 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) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
# 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 = {}) ⇒ Object

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')


86
87
88
89
90
# 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?Boolean

Returns true if no #file_operations are defined.



93
94
95
# File 'lib/file_pipeline/pipeline.rb', line 93

def empty?
  file_operations.empty?
end

#run(operation, versioned_file) ⇒ Object

Applies operation to versioned_file.

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



101
102
103
104
105
# 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