Class: BulkProcessor::CSVProcessor::RowProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_processor/csv_processor/row_processor.rb

Overview

An abstract implementation of the RowProcessor role. This class implements ‘#results` by returning an array of `Results`. To subclass, just implement `#process` to handle the row.

The row will be considered a failure by default. After a row is successfully processed, set ‘self.successful = true`. If there are any messages that should be passed back to the Handler, add them to the `#errors` array.

You can optionally override ‘#primary_keys` so that the result returned has more natural identifiers than just the row number. For example, you setting this to [’species’, ‘name’] (for the PetRowProcessor example from the README), the result would have ‘#primary_attributes` like

{ 'species' => 'dog', 'name' => 'Fido' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, row_num:, payload:) ⇒ RowProcessor



21
22
23
24
25
26
27
# File 'lib/bulk_processor/csv_processor/row_processor.rb', line 21

def initialize(row, row_num:, payload:)
  @row = row
  @row_num = row_num
  @payload = payload
  @successful = false
  @messages = []
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



19
20
21
# File 'lib/bulk_processor/csv_processor/row_processor.rb', line 19

def messages
  @messages
end

Instance Method Details

#process!Object

Raises:

  • (NotImplementedError)


29
30
31
32
# File 'lib/bulk_processor/csv_processor/row_processor.rb', line 29

def process!
  raise NotImplementedError,
        "#{self.class.name} must implement #{__method__}"
end

#resultObject



38
39
40
41
# File 'lib/bulk_processor/csv_processor/row_processor.rb', line 38

def result
  Result.new(messages: messages, row_num: row_num,
             primary_attributes: primary_attrs, successful: @successful)
end

#successful?Boolean



34
35
36
# File 'lib/bulk_processor/csv_processor/row_processor.rb', line 34

def successful?
  @successful
end