Module: Eco::Data::Locations::Convert

Includes:
Language::AuxiliarLogger
Included in:
NodeBase::Parsing, NodeBase::Serial, NodeLevel::Cleaner, NodeLevel::Parsing, NodeLevel::Serial
Defined in:
lib/eco/data/locations/convert.rb

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

#csv_from(filename, encoding: 'utf-8') ⇒ Eco::CSV::Table

Note:

this is a shortcut helper.

Helper to open a csv

Parameters:

  • filename (String)

    the csv file.

Returns:



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

def csv_from(filename, encoding: 'utf-8')
  raise ArgumentError, "Expecting String filename. Given: #{filename.class}" unless filename.is_a?(String)
  raise "Missing #{filename}" unless File.exists?(filename)
  Eco::CSV.read(filename, encoding: encoding)
rescue CSV::MalformedCSVError => e
  if match = e.message.match(/line (?<line>\d+)/i)
    log(:error) {"An encoding problem was found on line #{match[:line]}"}
  end
  raise
end

#empty_array(count) ⇒ Array<NilClass>

Returns with count positions.

Parameters:

  • count (Integer)

    number of possitions of the new array

Returns:

  • (Array<NilClass>)

    with count positions.



60
61
62
# File 'lib/eco/data/locations/convert.rb', line 60

def empty_array(count)
  Array.new(count, nil)
end

#empty_level_tracker_hash(count = 11) ⇒ Hash

Note:
  1. Initially it has as many keys as levels count
  2. It serves the purpose to track the lastest seen node for a given level, during a loop.

Returns with integer level counts as keys and nodes as values.

Returns:

  • (Hash)

    with integer level counts as keys and nodes as values.



70
71
72
# File 'lib/eco/data/locations/convert.rb', line 70

def empty_level_tracker_hash(count = 11)
  Array(1..count).zip(empty_array(count)).to_h
end

#hash_tree_to_tree_csv(hash_nodes, out: [], done_ids: [], repeated_ids: [], level: 0) ⇒ CSV::Table

Note:

The steps of usage would be:

  1. First treeify your input (i.e. Eco::API::Organization::TagTree#as_json, or treeify(nodes)

Generic converter/helper to generate the csv data export for a hierarchical csv tree

Parameters:

  • hash_nodes (Array<Hash>)

    a hierarchical tree of Hash nodes, nested via nodes

Returns:

  • (CSV::Table)

    ready to be made a hierarchical csv tree (i.e. out.to_csv)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/eco/data/locations/convert.rb', line 26

def hash_tree_to_tree_csv(hash_nodes, out: [], done_ids: [], repeated_ids: [], level: 0)
  lev  = level + 1
  base = empty_array(level)

  hash_nodes.each_with_object(out) do |node, out|
    if done_ids.include?(id = node["id"])
      repeated_ids << id
    else
      has_offspring = (children = node["nodes"]) && !children.empty?
      done_ids << id
      out      << (base.dup << node["id"])
      hash_tree_to_tree_csv(node["nodes"], out: out, done_ids: done_ids, repeated_ids: repeated_ids, level: lev)
    end
  end.tap do |out|
    if level == 0
      report_repeated_node_ids(repeated_ids)
      return Eco::CSV::Table.new(normalize_arrays(out))
    end
  end
end

#log_pretty_inspect(object, lev = :info) ⇒ Object

Note:

it only works where object is Enumberable

It logs a message from yield and appends a pretty_inspect on object.



76
77
78
79
80
81
82
83
# File 'lib/eco/data/locations/convert.rb', line 76

def log_pretty_inspect(object, lev = :info)
  return unless object.is_a?(Enumerable)
  return if object.empty?
  msg  = ''
  msg << "#{yield(object)}\n" if block_given?
  msg << object.pretty_inspect
  log(lev) { msg }
end

#normalize_arrays(rows) ⇒ Array<Array>

It normalizes the size of the arrays to the max size among the arrays

Parameters:

  • rows (Array<Array>)

    where arrays may not have the same length

Returns:

  • (Array<Array>)

    where arrays have all the same length



50
51
52
53
54
55
56
# File 'lib/eco/data/locations/convert.rb', line 50

def normalize_arrays(rows)
  max_row = rows.max {|a, b| a.length <=> b.length}
  holder  = empty_array(max_row.length)
  rows.map do |row|
    row.dup.concat(holder[0..-(row.length+1)])
  end
end

#report_repeated_node_ids(repeated) ⇒ Object

Prints a common message



86
87
88
89
90
# File 'lib/eco/data/locations/convert.rb', line 86

def report_repeated_node_ids(repeated)
  log_pretty_inspect(repeated, :warn) do
    "There were #{repeated.length} repeated node ids. Only one included. These excluded:"
  end
end