module DeepCover::Reporter::Tree::Util

Utility functions to deal with trees

Public Instance Methods

deep_merge(trees) click to toggle source
[{a: {b: {c: {} } } }
 {a: {b: {d: {} } } }]

> {a: {b: {c: {}, d: {} }}}

# File lib/deep_cover/reporter/tree/util.rb, line 53
def deep_merge(trees)
  trees.inject({}) do |result, h|
    result.merge(h) { |k, val, val_b| deep_merge([val, val_b]) }
  end
end
list_to_twig(items) click to toggle source

A twig is a tree with only single branches

a, b, c

>

{a: {b: {c: {} } } }
# File lib/deep_cover/reporter/tree/util.rb, line 42
def list_to_twig(items)
  result = {}
  items.inject(result) do |parent, value|
    parent[value] = {}
  end
  result
end
path_to_partial_paths(path) click to toggle source

'some/example/path' => %w[some example path]

# File lib/deep_cover/reporter/tree/util.rb, line 35
def path_to_partial_paths(path)
  path.to_s.split('/')
end
paths_to_tree(paths) click to toggle source
# File lib/deep_cover/reporter/tree/util.rb, line 25
def paths_to_tree(paths)
  twigs = paths.map do |path|
    partials = path_to_partial_paths(path)
    list_to_twig(partials)
  end
  tree = deep_merge(twigs)
  simplify(tree)
end
populate(tree, dir = '') { |full_path, path, children| ... } click to toggle source

{a: {b: {}}} => [ra, rb] where rb = yield('a/b', 'b', []) and ra = yield('a', 'a', [rb])

# File lib/deep_cover/reporter/tree/util.rb, line 75
def populate(tree, dir = '', &block)
  return to_enum(__method__, tree, dir) unless block_given?
  tree.map do |path, children_hash|
    full_path = [dir, path].join
    children = populate(children_hash, "#{full_path}/", &block)
    yield full_path, path, children
  end
end
populate_from_map(tree:, map:, merge:) { |full_path, partial_path, data, child_results || []| ... } click to toggle source
# File lib/deep_cover/reporter/tree/util.rb, line 10
def populate_from_map(tree:, map:, merge:)
  return to_enum(__method__, tree: tree, map: map, merge: merge) unless block_given?
  final_results, _final_data = populate(tree) do |full_path, partial_path, children|
    if children.empty?
      data = map.fetch(full_path)
    else
      child_results, child_data = children.transpose
      data = merge.call(child_data)
    end
    result = yield full_path, partial_path, data, child_results || []
    [result, data]
  end.transpose
  final_results
end
simplify(tree) click to toggle source

{a: {b: {c: {}, d: {} }}}

> {a/b: {c: {}, d: {} }}

# File lib/deep_cover/reporter/tree/util.rb, line 61
def simplify(tree)
  tree.map do |key, sub_tree|
    sub_tree = simplify(sub_tree)
    if sub_tree.size == 1
      key2, sub_tree = sub_tree.first
      key = "#{key}/#{key2}"
    end
    [key, sub_tree]
  end.to_h
end