Class: Burner::Library::IO::RowReader

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

Overview

Iterates over an array of objects, extracts a filepath from a key in each object, and attempts to load the file’s content for each record. The file’s content will be stored at the specified data_key. By default missing paths or files will be treated as hard errors. If you wish to ignore these then pass in true for ignore_blank_path and/or ignore_file_not_found.

Expected Payload input: array of objects. Payload output: array of objects.

Defined Under Namespace

Classes: FileNotFoundError

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(binary: false, data_key: DEFAULT_DATA_KEY, disk: {}, ignore_blank_path: false, ignore_file_not_found: false, name: '', path_key: DEFAULT_PATH_KEY, register: DEFAULT_REGISTER, separator: '') ⇒ RowReader

Returns a new instance of RowReader.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/burner/library/io/row_reader.rb', line 37

def initialize(
  binary: false,
  data_key: DEFAULT_DATA_KEY,
  disk: {},
  ignore_blank_path: false,
  ignore_file_not_found: false,
  name: '',
  path_key: DEFAULT_PATH_KEY,
  register: DEFAULT_REGISTER,
  separator: ''
)
  super(name: name, register: register)

  @binary                = binary || false
  @data_key              = data_key.to_s
  @disk                  = Disks.make(disk)
  @ignore_blank_path     = ignore_blank_path || false
  @ignore_file_not_found = ignore_file_not_found || false
  @path_key              = path_key.to_s
  @resolver              = Objectable.resolver(separator: separator)

  freeze
end

Instance Attribute Details

#binaryObject (readonly)

Returns the value of attribute binary.



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

def binary
  @binary
end

#data_keyObject (readonly)

Returns the value of attribute data_key.



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

def data_key
  @data_key
end

#diskObject (readonly)

Returns the value of attribute disk.



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

def disk
  @disk
end

#ignore_blank_pathObject (readonly)

Returns the value of attribute ignore_blank_path.



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

def ignore_blank_path
  @ignore_blank_path
end

#ignore_file_not_foundObject (readonly)

Returns the value of attribute ignore_file_not_found.



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

def ignore_file_not_found
  @ignore_file_not_found
end

#path_keyObject (readonly)

Returns the value of attribute path_key.



29
30
31
# File 'lib/burner/library/io/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/io/row_reader.rb', line 29

def resolver
  @resolver
end

Instance Method Details

#perform(output, payload) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/burner/library/io/row_reader.rb', line 61

def perform(output, payload)
  records = array(payload[register])

  output.detail("Reading path_key: #{path_key} for #{payload[register].length} records(s)")
  output.detail("Storing read data in: #{path_key}")

  payload[register] = records.map.with_index(1) do |object, index|
    load_data(object, index, output)
  end
end