class SimilarityTree::Node

Attributes

children[RW]
content[RW]
diff_score[RW]
id[RW]
parent[RW]

Public Class Methods

new(id, diff_score, parent = nil, children = [], content = nil) click to toggle source
# File lib/similarity_tree/node.rb, line 6
def initialize(id, diff_score, parent = nil, children = [], content = nil)
  @id, @diff_score, @parent, @children, @content = id, diff_score, parent, children, content
end

Public Instance Methods

each_node() { |n| ... } click to toggle source

self and all descendents

# File lib/similarity_tree/node.rb, line 11
def each_node
  depth_first_recurse{|n, depth| yield n}
end
to_h() click to toggle source
# File lib/similarity_tree/node.rb, line 25
def to_h
  result = {
      id: id
  }
  result[:children] = children.map {|c| c.to_h} unless children.nil? || children.empty?
  result[:diff_score] = diff_score unless diff_score.nil?

  # if the content node has an as_json function, insert these attributes
  if content.respond_to?(:as_json) && content.as_json.is_a?(Hash)
    result[:content] = content.as_json.merge(result)
  end
  result
end
to_json(opts = {}) click to toggle source
# File lib/similarity_tree/node.rb, line 39
def to_json(opts = {})
  JSON.generate to_h, opts
end
to_s() click to toggle source
# File lib/similarity_tree/node.rb, line 15
def to_s
  str = ""
  depth_first_recurse do |n, depth|
    str += ("-" * depth) + n.id.to_s
    str += ' (' + n.diff_score.to_s + ')' unless n.diff_score.nil?
    str += "\n"
  end
  str
end

Private Instance Methods

depth_first_recurse(node = nil, depth = 0) { |node, depth| ... } click to toggle source

helper for recursion into descendents

# File lib/similarity_tree/node.rb, line 45
def depth_first_recurse(node = nil, depth = 0, &block)
  node = self if node == nil
  yield node, depth
  node.children.each do |child|
    depth_first_recurse(child, depth+1, &block)
  end
end