class Burner::Library::Compress::RowReader

Iterates over an array of objects, extracts a path and data in each object, and creates a zip file. By default, if a path is blank then an ArgumentError will be raised. If this is undesirable then you can set ignore_blank_path to true and the record will be skipped. You also have the option to supress blank files being added by configuring ignore_blank_data as true.

Expected Payload input: array of objects. Payload output: compressed binary zip file contents.

Constants

Content
DEFAULT_DATA_KEY
DEFAULT_PATH_KEY

Attributes

data_key[R]
ignore_blank_data[R]
ignore_blank_path[R]
path_key[R]
resolver[R]

Public Class Methods

new( data_key: DEFAULT_DATA_KEY, ignore_blank_data: false, ignore_blank_path: false, name: '', path_key: DEFAULT_PATH_KEY, register: DEFAULT_REGISTER, separator: '' ) click to toggle source
Calls superclass method Burner::JobWithRegister::new
# File lib/burner/library/compress/row_reader.rb, line 35
def initialize(
  data_key: DEFAULT_DATA_KEY,
  ignore_blank_data: false,
  ignore_blank_path: false,
  name: '',
  path_key: DEFAULT_PATH_KEY,
  register: DEFAULT_REGISTER,
  separator: ''
)
  super(name: name, register: register)

  @data_key          = data_key.to_s
  @ignore_blank_data = ignore_blank_data || false
  @ignore_blank_path = ignore_blank_path || false
  @path_key          = path_key.to_s
  @resolver          = Objectable.resolver(separator: separator)

  freeze
end

Public Instance Methods

perform(output, payload) click to toggle source
# File lib/burner/library/compress/row_reader.rb, line 55
def perform(output, payload)
  payload[register] = Zip::OutputStream.write_buffer do |zip|
    array(payload[register]).each.with_index(1) do |record, index|
      content = extract_path_and_data(record, index, output)

      next unless content

      zip.put_next_entry(content.path)
      zip.write(content.data)
    end
  end.string
end

Private Instance Methods

assert_and_skip_missing_path?(path, index, output) click to toggle source
# File lib/burner/library/compress/row_reader.rb, line 84
def assert_and_skip_missing_path?(path, index, output)
  if ignore_blank_path && path.to_s.empty?
    output.detail("Skipping record #{index} because of blank path")
    true
  elsif path.to_s.empty?
    raise ArgumentError, "Record #{index} is missing a path at key: #{path_key}"
  end
end
extract_path_and_data(record, index, output) click to toggle source
# File lib/burner/library/compress/row_reader.rb, line 70
def extract_path_and_data(record, index, output)
  path = strip_leading_separator(resolver.get(record, path_key))
  data = resolver.get(record, data_key)

  return if assert_and_skip_missing_path?(path, index, output)
  return if skip_missing_data?(data, index, output)

  Content.new(path, data)
end
skip_missing_data?(data, index, output) click to toggle source
# File lib/burner/library/compress/row_reader.rb, line 93
def skip_missing_data?(data, index, output)
  return false unless ignore_blank_data && data.to_s.empty?

  output.detail("Skipping record #{index} because of blank data")
  true
end
strip_leading_separator(path) click to toggle source
# File lib/burner/library/compress/row_reader.rb, line 80
def strip_leading_separator(path)
  path.to_s.start_with?(File::SEPARATOR) ? path.to_s[1..-1] : path.to_s
end