Sha256: 9bfe22b8c4789a49f62a04a0c8686db6df9e4d531ea8de8531157832cfe1c588
Contents?: true
Size: 862 Bytes
Versions: 23
Compression:
Stored size: 862 Bytes
Contents
class Eco::API::UseCases::DefaultCases::CsvToTree module Treeify def treeify(nodes, &block) get_children(nil, parents_hash(nodes), &block) end private def parents_hash(nodes) nodes.each_with_object({}) do |node, parents| (parents[node.parentId] ||= []).push(node) end end def get_children(node_id, parents, &block) (parents[node_id] ||= []).each_with_object([]) do |child, results| node_hash = { "id" => child.id, "name" => child.name } if block_given? yield_hash = yield(child) node_hash.merge(yield_hash) if yield_hash.is_a?(Hash) end results << node_hash.merge({ "nodes" => get_children(child.id, parents, &block).compact }) end end end end
Version data entries
23 entries across 23 versions & 1 rubygems