Module: Eco::Data::Locations::NodeBase::Parsing

Includes:
Convert, Treeify
Included in:
Builder, CsvConvert
Defined in:
lib/eco/data/locations/node_base/parsing.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Treeify

#serialize_node, #treeify

Methods included from Language::AuxiliarLogger

#log

Methods included from Convert

#csv_from, #empty_array, #empty_level_tracker_hash, #hash_tree_to_tree_csv, #log_pretty_inspect, #normalize_arrays, #report_repeated_node_ids

Instance Method Details

#csv_nodes_from(filename, encoding: 'utf-8') ⇒ Array<NodePlain>, Array<NodeLevel>

Shortcut to obtain a list of parsed nodes out of a file

Parameters:

  • filename (String)

    the csv file.

Returns:



28
29
30
# File 'lib/eco/data/locations/node_base/parsing.rb', line 28

def csv_nodes_from(filename, encoding: 'utf-8')
  nodes_from_csv(csv_from(filename, encoding: encoding))
end

#hash_tree_from_csv(csv) {|node, json| ... } ⇒ Array<Hash>

Returns a hierarchical tree of nested Hashes via nodes key.

Yields:

  • (node, json)

    optional custom serializer

Yield Parameters:

  • node (Node)

    the node that is being serialized

  • json (Hash)

    the default serialization

Yield Returns:

  • (Hash)

    the serialized Node

Returns:

  • (Array<Hash>)

    a hierarchical tree of nested Hashes via nodes key.

Raises:

  • (ArgumentError)


20
21
22
23
# File 'lib/eco/data/locations/node_base/parsing.rb', line 20

def hash_tree_from_csv(csv, &block)
  raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
  treeify(nodes_from_csv(csv), &block)
end

#nodes_from_csv(csv) ⇒ Array<NodePlain>, Array<NodeLevel>

Returns with integrity issues resolved.

Parameters:

Returns:

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/eco/data/locations/node_base/parsing.rb', line 8

def nodes_from_csv(csv)
  raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table)
  return Eco::Data::Locations::NodePlain.nodes_from_csv(csv) if Eco::Data::Locations::NodePlain.csv_matches_format?(csv)
  return Eco::Data::Locations::NodeLevel.nodes_from_csv(csv) if Eco::Data::Locations::NodeLevel.csv_matches_format?(csv)
  raise ArgumentError, "The input csv does not have the required format to read a locations structure."
end