Class: FilePipeline::FileOperations::Results

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

Overview

This class contains the results from a FileOperation being run on a file. Instances will be returned by the FileOperation#run method.

Instances contain the file operation opbject that has produced self, a flag for success, and any logs and data the operation may return.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, success, log_data) ⇒ Results

Returns a new instance.

Arguments
  • operation - Must respond to :name and :options.

  • success - true or false.

  • log_data - A string, error, array, hash, or nil.

Examples
error = StandardError.new
warning = 'a warning occurred'
log = [error, warning]
data = { mime_type: 'image/jpeg' }

my_op = MyOperation.new

Results.new(my_op, false, error)
# => <Results @data=nil, @log=[error], ..., @success=false>

Results.new(my_op, true, warning)
# => <Results @data=nil, @log=[warning], ..., @success=true>

Results.new(my_op, true, data)
# => <Results @data=data, @log=[], ..., @success=true>

Results.new(my_op, true, [warning, data])
# => <Results @data=data, @log=[warning], ..., @success=true>

Results.new(my_op, false, log)
# => <Results @data=nil, @log=[error, warning], ..., @success=false>

Results.new(my_op, false, [log, data])
# => <Results @data=data, @log=[error, warning], ..., @success=false>

Results.new(my_op, false, nil)
# => <Results @data=nil, @log=nil, ..., @success=false>


63
64
65
66
67
# File 'lib/file_pipeline/file_operations/results.rb', line 63

def initialize(operation, success, log_data)
  @operation = operation
  @success = success
  @log, @data = LogDataParser.new log_data
end

Instance Attribute Details

#dataObject (readonly)

Hash with any data returned from an operation.



23
24
25
# File 'lib/file_pipeline/file_operations/results.rb', line 23

def data
  @data
end

#logObject (readonly)

Array with log messages from operations.



20
21
22
# File 'lib/file_pipeline/file_operations/results.rb', line 20

def log
  @log
end

#operationObject (readonly)

The object (usually an instance of a subclass of FileOperation) that created self



13
14
15
# File 'lib/file_pipeline/file_operations/results.rb', line 13

def operation
  @operation
end

#successObject (readonly)

true if the operation has finished and produced a version file, or false if it encountered an error that caused it to terminate.



17
18
19
# File 'lib/file_pipeline/file_operations/results.rb', line 17

def success
  @success
end

Class Method Details

.normalize_log_data(obj) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/file_pipeline/file_operations/results.rb', line 94

def self.normalize_log_data(obj)
  return unless obj

  Results.return_data(obj) ||
    Results.return_log_message(obj) ||
    Results.return_log(obj) ||
    Results.return_log_and_data(obj)
end

.return_data(obj) ⇒ Object

:nodoc:



69
70
71
# File 'lib/file_pipeline/file_operations/results.rb', line 69

def self.return_data(obj) # :nodoc:
  return [nil, obj] if obj.is_a? Hash
end

.return_log(obj) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
# File 'lib/file_pipeline/file_operations/results.rb', line 73

def self.return_log(obj) # :nodoc:
  flat_array = obj.is_a?(Array) &&
               obj.none? { |i| i.is_a?(Array) || i.is_a?(Hash) }
  return unless flat_array

  [obj]
end

.return_log_and_data(obj) ⇒ Object

:nodoc:



81
82
83
84
85
86
# File 'lib/file_pipeline/file_operations/results.rb', line 81

def self.return_log_and_data(obj) # :nodoc:
  log = obj.find { |i| !i.is_a? Hash }
  log = [log] unless log.is_a? Array
  data = obj.find { |i| i.is_a? Hash }
  [log, data]
end

.return_log_message(obj) ⇒ Object

:nodoc:



88
89
90
91
92
# File 'lib/file_pipeline/file_operations/results.rb', line 88

def self.return_log_message(obj) # :nodoc:
  return if obj.is_a?(Array) || obj.is_a?(Hash)

  [[obj]]
end

Instance Method Details

#failureObject

Returns true if the operation was not succesful, false otherwise.



104
105
106
# File 'lib/file_pipeline/file_operations/results.rb', line 104

def failure
  !success
end