class Burner::Library::IO::RowReader
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.
Constants
- DEFAULT_DATA_KEY
- DEFAULT_PATH_KEY
Attributes
binary[R]
data_key[R]
disk[R]
ignore_blank_path[R]
ignore_file_not_found[R]
path_key[R]
resolver[R]
Public Class Methods
new( 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: '' )
click to toggle source
Calls superclass method
Burner::JobWithRegister::new
# 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
Public Instance Methods
perform(output, payload)
click to toggle source
# 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
Private Instance Methods
assert_and_skip_file_not_found?(path, index, output)
click to toggle source
# File lib/burner/library/io/row_reader.rb, line 89 def assert_and_skip_file_not_found?(path, index, output) does_not_exist = !disk.exist?(path) file_not_found_raises_error = !ignore_file_not_found if file_not_found_raises_error && does_not_exist output.detail("Record #{index} path: '#{path}' does not exist, raising error") raise FileNotFoundError, "#{path} does not exist" elsif does_not_exist output.detail("Record #{index} path: '#{path}' does not exist, skipping") true end end
assert_and_skip_missing_path?(path, index, output)
click to toggle source
# File lib/burner/library/io/row_reader.rb, line 74 def assert_and_skip_missing_path?(path, index, output) missing_path = path.to_s.empty? blank_path_raises_error = !ignore_blank_path if missing_path && blank_path_raises_error output.detail("Record #{index} is missing a path, raising error") raise ArgumentError, "Record #{index} is missing a path" elsif missing_path output.detail("Record #{index} is missing a path") true end end
load_data(object, index, output)
click to toggle source
# File lib/burner/library/io/row_reader.rb, line 104 def load_data(object, index, output) path = resolver.get(object, path_key) return object if assert_and_skip_missing_path?(path, index, output) return object if assert_and_skip_file_not_found?(path, index, output) data = disk.read(path, binary: binary) resolver.set(object, data_key, data) object end