class Nutmeg::TagTree

Attributes

original[RW]

Public Class Methods

new(tree) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 5
def initialize(tree)
  raise "No valid data type" unless tree.class == Tree::TreeNode
  @original = tree
end

Public Instance Methods

get_path_to_node(tag_slug, path_so_far = []) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 37
def get_path_to_node(tag_slug, path_so_far = [])
  node = @original.select{|node|node.content[:slug] == tag_slug}.first
  return get_recursive_path_to_node(node, [node]).reverse
end
get_paths(tags_given, strict = false) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 16
def get_paths(tags_given, strict = false)
  start_nodes = @original.children.select{|l| tags_given.include?(l.content[:slug])}
  end_nodes = @original.each_leaf.select{|l| tags_given.include?(l.content[:slug])}

  result = end_nodes.select do |end_node|
    (end_node.parentage & start_nodes).count >= 1
  end.collect do |leaf|
    ([leaf] + leaf.parentage).reverse
  end.select do |path|
    (path.map{|tag| tag.content[:slug] } & tags_given).count >= 1
  end

  result.sort_by! do |path|
    ((path.map{|tag| tag.content[:slug] } & tags_given).count)
  end.reverse!
end
get_paths_eager(tags_given) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 33
def get_paths_eager(tags_given)
  @original.children.select{|l| tags_given.include?(l.content[:slug])}.collect{|leaf| ([leaf] + leaf.parentage).reverse }
end
get_paths_formatted(tags_given, seperator = "/") click to toggle source
# File lib/nutmeg/tag_tree.rb, line 48
def get_paths_formatted(tags_given, seperator = "/")
  get_paths(tags_given).collect do |tags|
    tags.reject{|tag| tag.content[:slug] == "root"}.map{|node| node.content[:slug]}.join(seperator)
  end
end
get_recursive_path_to_node(node, path_so_far) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 42
def get_recursive_path_to_node(node, path_so_far)
  return path_so_far if node.parent.nil? || node.parent.content[:slug] == 'root'
  path_so_far << node.parent
  get_recursive_path_to_node(node.parent, path_so_far)
end
subtree(slug) click to toggle source
# File lib/nutmeg/tag_tree.rb, line 10
def subtree(slug)
  @original.each do |node|
    return Nutmeg::TagTree.new(node.detached_subtree_copy) if node.content[:slug] == slug
  end
end