Module: Eco::Data::Locations::NodeLevel::Serial

Includes:
Convert, Cleaner
Included in:
Builder
Defined in:
lib/eco/data/locations/node_level/serial.rb

Instance Attribute Summary collapse

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

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

#log

Methods included from Cleaner

#done_ids, #fill_in_parents, #repeated_ids, #reset_trackers!, #tidy_nodes

Instance Attribute Details

#serializerProc

Returns the serializer to be used.

Returns:

  • (Proc)

    the serializer to be used.



9
10
11
12
13
14
15
16
17
# File 'lib/eco/data/locations/node_level/serial.rb', line 9

def serializer
  @serializer ||= proc do |node|
    raise "Expecting NodeLevel. Given: #{node.class}" unless node.is_a?(Eco::Data::Locations::NodeLevel)
    keys = %w[row_num id name]
    keys.zip(node.values_at(*keys)).to_h.tap do |out|
      out['parent_id'] = node.parentId
    end
  end
end

Instance Method Details

#nodes_to_csv_tree(nodes) ⇒ CSV::Table

Note:
  1. tidy_nodes is called out of consistency. For example, it might be that the parents are not set. After this call, all the integrity issues have been warned.
  2. NodeBase has this same class method. This one was kept to preserve its indepenence towards treeify.

Transforms nodes into a hierarchical csv tree.

Returns:

  • (CSV::Table)

    ready to dump into a hierarhical csv (columns are tree levels)



27
28
29
30
31
32
33
34
35
# File 'lib/eco/data/locations/node_level/serial.rb', line 27

def nodes_to_csv_tree(nodes)
  tidy_nodes(nodes).each_with_object([]) do |node, out|
    out         << (row = empty_array(node.actual_level))
    # replace last item with `node.id`
    row[-1..-1]  = [node.id]
  end.tap do |out|
    return Eco::CSV::Table.new(normalize_arrays(out))
  end
end