Module: Eco::Data::Locations::NodeLevel::Parsing
- Included in:
- Builder
- Defined in:
- lib/eco/data/locations/node_level/parsing.rb
Instance Attribute Summary collapse
Attributes included from Language::AuxiliarLogger
Instance Method Summary collapse
-
#csv_matches_format?(csv) ⇒ Boolean
It evaluates as an opposite to
NodePlain
. - #nodes_from_csv(csv) ⇒ Array<NodeLevel>
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
Methods included from Language::AuxiliarLogger
Methods included from Cleaner
#done_ids, #fill_in_parents, #repeated_ids, #reset_trackers!, #tidy_nodes
Instance Attribute Details
Instance Method Details
#csv_matches_format?(csv) ⇒ Boolean
Note:
there are only two accepted input csv formats.
The expected header of NodePlain
is predictable,
but the header of NodeLevel
is not.
It evaluates as an opposite to NodePlain
18 19 20 21 |
# File 'lib/eco/data/locations/node_level/parsing.rb', line 18 def csv_matches_format?(csv) return false unless csv.is_a?(::CSV::Table) !Eco::Data::Locations::NodePlain.csv_matches_format?(csv) end |
#nodes_from_csv(csv) ⇒ Array<NodeLevel>
Note:
- It ensures basic data integrity when builing the nodes in the first screening
- It then delegates the tidy up to a cleaner function (see
tidy_nodes
)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eco/data/locations/node_level/parsing.rb', line 28 def nodes_from_csv(csv) raise ArgumentError, "Expecting CSV::Table. Given: #{csv.class}" unless csv.is_a?(::CSV::Table) possible_classifications = csv.headers # Convert to Eco::CSV::Table for a fresh start csv = Eco::CSV.parse(csv.to_csv).nil_blank_cells.add_index_column(:row_num) prev_node = nil nodes = csv.each_with_object([]) do |row, out| row_num, *values = row.fields node = node_class.new(row_num, *values).tap do |nd| nd.original_headers = possible_classifications end out << node prev_node ||= node # unless top level # Make sure upper level tags are there (including parent) # This normalizes input to all upper level tags always filled in # which allows to node#actual_level to work node.set_high_levels(prev_node) unless node.raw_level == 1 prev_node = node end tidy_nodes(nodes) end |