Module: Eco::Data::Locations::NodePlain::Parsing

Included in:
Builder
Defined in:
lib/eco/data/locations/node_plain/parsing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#node_classObject



5
6
7
# File 'lib/eco/data/locations/node_plain/parsing.rb', line 5

def node_class
  @node_class ||= Eco::Data::Locations::NodePlain
end

Instance Method Details

#basic_headersObject

Subset of property names expected to be among the headers



10
11
12
# File 'lib/eco/data/locations/node_plain/parsing.rb', line 10

def basic_headers
  @basic_headers ||= %w[id name parent_id]
end

#csv_matches_format?(csv) ⇒ Boolean

Checks if the basic_headers to process as NodePlain are present.

Parameters:

Returns:

  • (Boolean)

    whether or not it's worthy trying to parse with NodePlain.



17
18
19
20
# File 'lib/eco/data/locations/node_plain/parsing.rb', line 17

def csv_matches_format?(csv)
  return false unless csv.is_a?(::CSV::Table)
  (basic_headers & csv.headers) == basic_headers
end

#nodes_from_csv(csv) ⇒ Array<NodePlain>

It builds each NodePlain from the input csv.

Parameters:

Returns:

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
# File 'lib/eco/data/locations/node_plain/parsing.rb', line 25

def nodes_from_csv(csv)
  raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
  # Convert to Eco::CSV::Table for a fresh start
  csv     = Eco::CSV.parse(csv.to_csv).nil_blank_cells.add_index_column(:row_num)
  headers = node_class::ALL_ATTRS.map(&:to_s)
  csv.each_with_object([]) do |row, out|
    row_num, *values = row.values_at(*headers)
    out << node_class.new(row_num, *values)
  end
end