Class: NcsNavigator::Warehouse::Transformers::MdesCsv::TableReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb

Overview

A streaming reader for a CSV containing one table's-worth of MDES data.

The CSV is converted into warehouse model instances, one record per row. The CSV MUST have a header row whose cells indicate the variable contained in each column.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, model_designator, filename) ⇒ TableReader

Returns a new instance of TableReader.

Parameters:

  • configuration (Configuration)
  • model_designator (Class, #to_s)

    a name for the expected record type from the current CSV. It can be a table name or unqualified warehouse model name, or an actual warehouse model class.

  • filename (String)

    the file containing the CSV



23
24
25
26
27
28
29
30
31
32
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 23

def initialize(configuration, model_designator, filename)
  @configuration = configuration
  @model =
    if Class === model_designator
      model_designator
    else
      configuration.model(model_designator.to_s)
    end
  @filename = filename
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



15
16
17
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 15

def configuration
  @configuration
end

#filenameObject (readonly)

Returns the value of attribute filename.



15
16
17
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 15

def filename
  @filename
end

#modelObject (readonly)

Returns the value of attribute model.



15
16
17
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 15

def model
  @model
end

Instance Method Details

#eachObject



38
39
40
41
42
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 38

def each
  CSV.foreach(filename, :headers => true, :header_converters => [:downcase]) do |row|
    yield create_instance_for_row(row)
  end
end

#nameObject



34
35
36
# File 'lib/ncs_navigator/warehouse/transformers/mdes_csv/table_reader.rb', line 34

def name
  "#{filename} => #{model.mdes_table_name} table"
end