Sha256: f34f9a1ca13fec915b6336283890066d15758f6741900a8f43e5e97706ae4dc5

Contents?: true

Size: 1.38 KB

Versions: 53

Compression:

Stored size: 1.38 KB

Contents

class Eco::Data::Locations::NodeLevel
  module Serial
    include Eco::Data::Locations::NodeLevel::Cleaner
    include Eco::Data::Locations::Convert

    attr_writer :serializer

    # @return [Proc] the serializer to be used.
    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

    # Transforms `nodes` into a **hierarchical** csv tree.
    # @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.
    # @return [CSV::Table] ready to dump into a hierarhical **csv** (columns are tree levels)
    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
  end
end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
eco-helpers-3.0.1 lib/eco/data/locations/node_level/serial.rb
eco-helpers-3.0.0 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.25 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.24 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.23 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.22 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.21 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.20 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.19 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.18 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.17 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.16 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.15 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.14 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.13 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.12 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.4 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.2 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.1 lib/eco/data/locations/node_level/serial.rb
eco-helpers-2.7.0 lib/eco/data/locations/node_level/serial.rb