Class: Burner::Library::Compress::RowReader

Inherits:
JobWithRegister show all
Defined in:
lib/burner/library/compress/row_reader.rb

Overview

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.

Constant Summary collapse

DEFAULT_DATA_KEY =
'data'
DEFAULT_PATH_KEY =
'path'

Constants inherited from JobWithRegister

JobWithRegister::BLANK

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Arrayable

#array

Constructor Details

#initialize(data_key: DEFAULT_DATA_KEY, ignore_blank_data: false, ignore_blank_path: false, name: '', path_key: DEFAULT_PATH_KEY, register: DEFAULT_REGISTER, separator: '') ⇒ RowReader

Returns a new instance of RowReader.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#data_keyObject (readonly)

Returns the value of attribute data_key.



29
30
31
# File 'lib/burner/library/compress/row_reader.rb', line 29

def data_key
  @data_key
end

#ignore_blank_dataObject (readonly)

Returns the value of attribute ignore_blank_data.



29
30
31
# File 'lib/burner/library/compress/row_reader.rb', line 29

def ignore_blank_data
  @ignore_blank_data
end

#ignore_blank_pathObject (readonly)

Returns the value of attribute ignore_blank_path.



29
30
31
# File 'lib/burner/library/compress/row_reader.rb', line 29

def ignore_blank_path
  @ignore_blank_path
end

#path_keyObject (readonly)

Returns the value of attribute path_key.



29
30
31
# File 'lib/burner/library/compress/row_reader.rb', line 29

def path_key
  @path_key
end

#resolverObject (readonly)

Returns the value of attribute resolver.



29
30
31
# File 'lib/burner/library/compress/row_reader.rb', line 29

def resolver
  @resolver
end

Instance Method Details

#perform(output, payload) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# 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