Class: NdrImport::Xml::Table

Inherits:
Table
  • Object
show all
Defined in:
lib/ndr_import/xml/table.rb

Overview

This class maintains the state of a xml 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.

Constant Summary collapse

XML_OPTIONS =
%w[pattern_match_record_xpath xml_file_metadata xml_record_xpath
yield_xml_record].freeze

Instance Attribute Summary

Attributes inherited from Table

#notifier, #table_metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Table

#all_valid_options, #encode_with, #header_valid?, #initialize, #match, #mutate_regexp_columns, #process_line, #transform

Constructor Details

This class inherits a constructor from NdrImport::Table

Class Method Details

.all_valid_optionsObject



16
17
18
# File 'lib/ndr_import/xml/table.rb', line 16

def self.all_valid_options
  super - %w[delimiter header_lines footer_lines] + XML_OPTIONS
end

Instance Method Details



26
27
28
# File 'lib/ndr_import/xml/table.rb', line 26

def footer_lines
  0
end

#header_linesObject



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

def header_lines
  0
end

#transform_line(line, index) {|records_from_xml_line.compact| ... } ⇒ Object

This method transforms an incoming line (element) of xml data by applying each of the klass masked mappings to the line and yielding the klass and fields for each mapped klass.

Yields:

  • (records_from_xml_line.compact)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ndr_import/xml/table.rb', line 33

def transform_line(line, index)
  return enum_for(:transform_line, line, index) unless block_given?
  raise 'Not an Nokogiri::XML::Element!' unless line.is_a? Nokogiri::XML::Element

  augmented_masked_mappings = augment_and_validate_column_mappings_for(line)

  xml_line = xml_line_from(line)

  records_from_xml_line = []
  augmented_masked_mappings.each do |klass, klass_mappings|
    fields = mapped_line(xml_line, klass_mappings)

    next if fields[:skip].to_s == 'true'.freeze

    if yield_xml_record
      records_from_xml_line << [klass, fields, index]
    else
      yield(klass, fields, index)
    end
  end
  yield(records_from_xml_line.compact) if yield_xml_record
end