Module: Eco::Data::Locations::NodeBase::CsvConvert

Includes:
Parsing
Included in:
Builder
Defined in:
lib/eco/data/locations/node_base/csv_convert.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Parsing

#csv_nodes_from, #hash_tree_from_csv, #nodes_from_csv

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_list(value) {|node, json| ... } ⇒ CSV::Table

Note:

it just converts to an organizational tagtree and uses a helper method.

Returns a table with a list of nodes and their parents.

Parameters:

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:

  • (CSV::Table)

    a table with a list of nodes and their parents



62
63
64
65
# File 'lib/eco/data/locations/node_base/csv_convert.rb', line 62

def csv_list(value, &block)
  value = org_tree(value, &block) unless value.is_a?(tree_class)
  Eco::CSV::Table.new(hash_list(value, &block))
end

#csv_tree(value, encoding: 'utf-8', attrs: [:id]) {|node, json| ... } ⇒ CSV::Table

Returns a table with L1 to Ln columns ready for dump to csv.

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:

  • (CSV::Table)

    a table with L1 to Ln columns ready for dump to csv



51
52
53
# File 'lib/eco/data/locations/node_base/csv_convert.rb', line 51

def csv_tree(value, encoding: 'utf-8', attrs: [:id], &block) # rubocop:disable Lint/UnusedMethodArgument
  Eco::CSV::Table.new(hash_tree_to_tree_csv(hash_tree(value, &block), attrs: attrs))
end

#hash_list(value) {|node, json| ... } ⇒ Array<Hash>

Returns a plain list of hash nodes.

Parameters:

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 plain list of hash nodes

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/eco/data/locations/node_base/csv_convert.rb', line 15

def hash_list(value, &block)
  return hash_list(org_tree(value), &block) if value.is_a?(::CSV::Table)
  return value.as_nodes_json(&block)        if value.is_a?(tree_class)
  raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}"
end

#hash_tree(value) {|node, json| ... } ⇒ Array<Hash>

Returns a hierarchical tree of hash nodes, ready to be parsed as an organization tagtree.

Parameters:

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 hash nodes, ready to be parsed as an organization tagtree

Raises:

  • (ArgumentError)


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

def hash_tree(value, &block)
  return hash_tree_from_csv(value, &block) if value.is_a?(::CSV::Table)
  return value.as_json(&block)             if value.is_a?(tree_class)
  raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}"
end

#org_tree(value) {|node, json| ... } ⇒ Eco::API::Organization::TagTree

Parameters:

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:

Raises:

  • (ArgumentError)


40
41
42
43
44
# File 'lib/eco/data/locations/node_base/csv_convert.rb', line 40

def org_tree(value, &block)
  return tree_class.new(hash_tree(value), &block) if value.is_a?(::CSV::Table)
  return tree_class.new(value.as_json)            if value.is_a?(tree_class)
  raise ArgumentError, "Expecting Eco::API::Organization::TagTree or CSV::Table. Given: #{value.class}"
end

#tree_classObject



5
6
7
# File 'lib/eco/data/locations/node_base/csv_convert.rb', line 5

def tree_class
  Eco::API::Organization::TagTree
end