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
47
48
49
# File 'lib/file_pipeline/pipeline.rb', line 43

def <<(file_operation_instance)
  unless file_operation_instance.respond_to? :run
    raise TypeError, 'File operations must implement a #run method'
  end

  @file_operations << file_operation_instance
end

#apply_to(versioned_file) ⇒ Object

Applies all #file_operations to a versioned_file and returns it.



52
53
54
55
# File 'lib/file_pipeline/pipeline.rb', line 52

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.



59
60
61
62
# File 'lib/file_pipeline/pipeline.rb', line 59

def batch_apply(versioned_files)
  versioned_files.map { |file| Thread.new(file) { apply_to(file) } }
                 .map(&:value)
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')


83
84
85
86
87
# File 'lib/file_pipeline/pipeline.rb', line 83

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.

Returns:

  • (Boolean)


90
91
92
# File 'lib/file_pipeline/pipeline.rb', line 90

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



98
99
100
101
102
# File 'lib/file_pipeline/pipeline.rb', line 98

def run(operation, versioned_file)
  versioned_file.modify do |version, directory, original|
    operation.run version, directory, original
  end
end