Class: NdrImport::NonTabular::Table

Inherits:
Table
  • Object
show all
Includes:
UTF8Encoding
Defined in:
lib/ndr_import/non_tabular/table.rb

Overview

This class maintains the state of a non tabular table mapping and encapsulates the logic required to transform a table of data into “records”. Particular attention has been made to use enumerables throughout to help with the transformation of large quantities of data.

Direct Known Subclasses

Mapping

Constant Summary collapse

NON_TABULAR_OPTIONS =
%w(capture_start_line start_line_pattern end_line_pattern remove_lines
start_in_a_record end_in_a_record)

Instance Attribute Summary collapse

Attributes inherited from Table

#notifier

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Table

#all_valid_options, #header_valid?, #match, #process_line, #transform_line

Constructor Details

#initialize(options = {}) ⇒ Table

Returns a new instance of Table.



37
38
39
40
41
# File 'lib/ndr_import/non_tabular/table.rb', line 37

def initialize(options = {})
  super(options)

  validate_presence_of_start_line_pattern
end

Instance Attribute Details

#non_tabular_linesObject

Returns the value of attribute non_tabular_lines.



27
28
29
# File 'lib/ndr_import/non_tabular/table.rb', line 27

def non_tabular_lines
  @non_tabular_lines
end

Class Method Details

.all_valid_optionsObject



22
23
24
# File 'lib/ndr_import/non_tabular/table.rb', line 22

def self.all_valid_options
  super - %w(tablename_pattern header_lines footer_lines) + NON_TABULAR_OPTIONS
end

Instance Method Details



33
34
35
# File 'lib/ndr_import/non_tabular/table.rb', line 33

def footer_lines
  0
end

#header_linesObject



29
30
31
# File 'lib/ndr_import/non_tabular/table.rb', line 29

def header_lines
  0
end

#tablename_pattern=(_value) ⇒ Object



43
44
45
# File 'lib/ndr_import/non_tabular/table.rb', line 43

def tablename_pattern=(_value)
  fail NdrImport::MappingError, 'Should not define tablename_pattern'
end

#transform(lines, &block) ⇒ Object

This method transforms a table of data, given a line array/enumerator and yields klass, fields and index (input row number) for each record that it would create as a result of the transformation process.



56
57
58
59
60
61
62
63
# File 'lib/ndr_import/non_tabular/table.rb', line 56

def transform(lines, &block)
  return enum_for(:transform, lines) unless block

  self.non_tabular_lines = ensure_utf8_enum!(lines)
  remove_unwanted_lines

  super(read_non_tabular_array, &block)
end

#validate_header(_line, _column_mappings) ⇒ Object



65
66
67
# File 'lib/ndr_import/non_tabular/table.rb', line 65

def validate_header(_line, _column_mappings)
  @header_valid = true
end

#validate_presence_of_start_line_patternObject



47
48
49
50
51
# File 'lib/ndr_import/non_tabular/table.rb', line 47

def validate_presence_of_start_line_pattern
  return if @start_line_pattern
  fail NdrImport::MappingError,
       I18n.t('mapping.errors.missing_start_line_pattern')
end