Sha256: 42ac71cff5a9ebed08a6483b3ffe68e186d509fa79f737427fc685e81c162951

Contents?: true

Size: 704 Bytes

Versions: 5

Compression:

Stored size: 704 Bytes

Contents

module Ecoportal
  module API
    class GraphQL
      module Helpers
        module TagTree
          def treeify(nodes)
            parents = nodes.each_with_object({}) do |node, parents|
              (parents[node.parentId] ||= []).push(node)
            end
            get_children(nil, parents)
          end

          private

          def get_children(node_id, parents)
            (parents[node_id] ||= []).each_with_object([]) do |child, results|
              results << {
                "tag" =>   child.tag,
                "nodes" => get_children(child.id, parents).compact
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ecoportal-api-graphql-0.1.11 lib/ecoportal/api/graphql/helpers/tag_tree.rb
ecoportal-api-graphql-0.1.10 lib/ecoportal/api/graphql/helpers/tag_tree.rb
ecoportal-api-graphql-0.1.9 lib/ecoportal/api/graphql/helpers/tag_tree.rb
ecoportal-api-graphql-0.1.8 lib/ecoportal/api/graphql/helpers/tag_tree.rb
ecoportal-api-graphql-0.1.7 lib/ecoportal/api/graphql/helpers/tag_tree.rb