class FilePipeline::Versions::History

History objects keep track of a VersionedFile instances versions names and any associated logs or data for each version.

Public Class Methods

new() click to toggle source

Returns a new instance.

# File lib/file_pipeline/versions/history.rb, line 9
def initialize
  @entries = {}
end

Public Instance Methods

[](version_name) click to toggle source

Retrieves the results object for the version_name.

# File lib/file_pipeline/versions/history.rb, line 14
def [](version_name)
  @entries[version_name]
end
[]=(version_name, results) click to toggle source

Associates the results with the version_name.

# File lib/file_pipeline/versions/history.rb, line 19
def []=(version_name, results)
  entry = @entries.fetch version_name, []
  entry << results
  @entries[version_name] = entry.compact
end
captured_data() click to toggle source

Returns a two-dimensional array, where each nested array has two items:

  • the file operation object

  • data captured by the operartion (if any).

[[file_operation_object, data_or_nil], ...]

# File lib/file_pipeline/versions/history.rb, line 30
def captured_data
  filter :data
end
captured_data_for(operation_name, **options) click to toggle source

Returns any data captured by operation_name.

If multiple instances of one operation class have modified the file, pass any options the specific instance of the operation was initialized with as the optional second argument.

# File lib/file_pipeline/versions/history.rb, line 39
def captured_data_for(operation_name, **options)
  return if empty?

  captured_data.filter { |op, _| matches? op, operation_name, options }
               .map(&:last)
end
captured_data_with(tag) click to toggle source

Returns an array with all data captured by operations with tag. Returns an empty array if there is no data for tag.

Tags are defined in FileOperations::CapturedDataTags

# File lib/file_pipeline/versions/history.rb, line 50
def captured_data_with(tag)
  captured_data.filter { |op, _| op.captured_data_tag == tag }
               .map(&:last)
end
clear!() click to toggle source

Clears all history entries (version names and associated results).

# File lib/file_pipeline/versions/history.rb, line 56
def clear!
  @entries.clear
end
empty?() click to toggle source

Returns true if self has no entries (version names and associated results), true otherwise.

# File lib/file_pipeline/versions/history.rb, line 62
def empty?
  @entries.empty?
end
log() click to toggle source

Returns an array of triplets (arryas with three items each):

* Name of the file operation class (String).
* Options for the file operation instance (Hash).
* The log (Array).
# File lib/file_pipeline/versions/history.rb, line 70
def log
  filter(:log).map { |op, results| [op.name, op.options, results] }
end
to_a() click to toggle source

Returns a two-dimensional Array where every nested Array will consist of the version name (file path) at index 0 and nil or an Array with all results objects for the version at index 1:

[version_name, [results1, ...]]

# File lib/file_pipeline/versions/history.rb, line 79
def to_a
  @entries.to_a
end
versions() click to toggle source

Returns an array with paths to the version files of self (excluding original).

# File lib/file_pipeline/versions/history.rb, line 85
def versions
  @entries.keys
end

Private Instance Methods

filter(item) click to toggle source

Filters entries in self by item (:log or :data).

# File lib/file_pipeline/versions/history.rb, line 92
def filter(item)
  @entries.values.flatten.select(&item).map do |results|
    [results.operation, results.public_send(item)]
  end
end
matches?(operation, name, opts) click to toggle source

Returns true if name matches the name attribute of operation and options matches the options the operation instance is initialized with.

# File lib/file_pipeline/versions/history.rb, line 101
def matches?(operation, name, opts)
  operation.name == name && opts.all? { |k, v| operation.options[k] == v }
end