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
Instance Method Summary collapse
-
#csv_from(filename, encoding: 'utf-8') ⇒ Eco::CSV::Table
Helper to open a csv.
-
#empty_array(count) ⇒ Array<NilClass>
With
count
positions. -
#empty_level_tracker_hash(count = 11) ⇒ Hash
With integer level counts as keys and nodes as values.
-
#hash_tree_to_tree_csv(hash_nodes, out: [], done_ids: [], repeated_ids: [], level: 0, attrs: [:id]) {|str_node, node| ... } ⇒ CSV::Table
Generic converter/helper to generate the csv data export for a hierarchical csv tree.
-
#log_pretty_inspect(object, lev = :info) ⇒ Object
It logs a message from
yield
and appends apretty_inspect
on object. -
#normalize_arrays(rows) ⇒ Array<Array>
It normalizes the size of the arrays to the max size among the arrays.
-
#report_repeated_node_ids(repeated) ⇒ Object
Prints a common message.
Methods included from Language::AuxiliarLogger
Instance Method Details
#csv_from(filename, encoding: 'utf-8') ⇒ Eco::CSV::Table
this is a shortcut helper.
Helper to open a csv
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..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.
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
- Initially it has as many keys as levels
count
- 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.
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
The steps of usage would be:
- First treeify your input (i.e.
Eco::API::Organization::TagTree#as_json
, ortreeify(nodes)
Generic converter/helper to generate the csv data export for a hierarchical csv tree
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
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
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 |