Class: NcsNavigator::Warehouse::Transformers::VdrXml::Reader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb

Overview

A streaming reader for MDES VDR XML-formatted files. It reads each record in turn, converts it into a warehouse model instance, and yields it to a provided collaborator.

Constant Summary collapse

MISSING_CODES =

These code values indicate missing or unknown data. If one of them appears in a nullable, non-coded field, we will skip it. Similarly, if a record's PK is one of these values, we will skip the entire record.

%w(-3 -4 -6 -7)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, filename_or_io, options = {}) ⇒ Reader

Returns a new instance of Reader.



31
32
33
34
35
36
37
38
39
40
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 31

def initialize(config, filename_or_io, options={})
  @configuration = config
  @filename, @io = case filename_or_io
                   when String
                     [filename_or_io, File.open(filename_or_io)]
                   else
                     [nil, filename_or_io]
                   end
  @record_count = 0
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



15
16
17
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 15

def configuration
  @configuration
end

#filenameObject (readonly)

Returns the value of attribute filename.



16
17
18
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 16

def filename
  @filename
end

#record_countFixnum (readonly)

Returns the number of records that have been read so far.

Returns:

  • (Fixnum)

    the number of records that have been read so far.



29
30
31
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 29

def record_count
  @record_count
end

Instance Method Details

#each { ... }

This method returns an undefined value.

Reads the XML and yields each encountered record as a warehouse model instance to the provided block. It does not validate the records and only performs minimal transformation (chiefly removing foreign key values which actually indicate a missing child record).

Yields:

  • a series of model instances



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 59

def each(&block)
  shell.say_line("Reading VDR XML #{filename if filename}")
  @start = Time.now

  Nokogiri::XML::Reader(@io).each do |node|
    element_types = [:TYPE_ELEMENT, :TYPE_END_ELEMENT].
      collect { |c| Nokogiri::XML::Reader.const_get(c) }
    next unless element_types.include?(node.node_type)
    encounter_node(node, node.node_type == 1, &block)
  end

  @end = Time.now

  shell.clear_line_then_say(
    "Completed read of %6d records in %d seconds (%3.1f per second)\n" %
    [@record_count, load_time, load_rate])
ensure
  @io.close if filename
end

#nameObject



42
43
44
45
46
47
48
# File 'lib/ncs_navigator/warehouse/transformers/vdr_xml/reader.rb', line 42

def name
  if @filename
    "VDR XML #{@filename}"
  else
    "VDR XML #{@io.inspect}"
  end
end