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.exist?(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.



64
65
66
# File 'lib/eco/data/locations/convert.rb', line 64

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.



74
75
76
# File 'lib/eco/data/locations/convert.rb', line 74

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, attrs: [:id]) {|str_node, node| ... } ⇒ 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

Yields:

  • (str_node, node)

    block for custom output node name

Returns:

  • (CSV::Table)

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



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

def hash_tree_to_tree_csv(hash_nodes, out: [], done_ids: [], repeated_ids: [], level: 0, attrs: [:id])
  lev  = level + 1
  base = empty_array(level)
  sattrs = attrs.map(&:to_s)

  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
      str_node = node.values_at(*sattrs).join(" ## ")
      out      << (base.dup << str_node)
      hash_tree_to_tree_csv(node["nodes"], out: out, done_ids: done_ids, repeated_ids: repeated_ids, level: lev, attrs: attrs)
    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 Enumerable

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



80
81
82
83
84
85
86
87
# File 'lib/eco/data/locations/convert.rb', line 80

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



54
55
56
57
58
59
60
# File 'lib/eco/data/locations/convert.rb', line 54

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



90
91
92
93
94
# File 'lib/eco/data/locations/convert.rb', line 90

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