class WhiteCloth::DataStructures::StandardTree

The {StandardTree} is, as the name implies, the basic data structure for a realm. 
All mutations operate over a {StandardTree}, and all projections to and from the

 void create or imply a {StandardTree} as a result.

Public Class Methods

new(root_id = nil) click to toggle source

Default constructor. Create a new tree, with a single node at the root.

# File lib/data_structures/standard_tree.rb, line 30
def initialize(root_id = nil)
  # Create the root id from supplied +root_id+, or a random one if the
  # +root_id+ is missing
  unless root_id.nil?
    begin
      @root_id = UUIDTools::UUID.parse(root_id)
    rescue
      @root_id = UUIDTools::UUID.random_create
    end
  else
    @root_id = UUIDTools::UUID.random_create
  end
  @next_id = @root_id.next 
  
  # Create the root node for the tree
  @nodes = StandardNode.new(@root_id.to_s, nil)
end

Public Instance Methods

<<(node_list) click to toggle source

Adds the specified nodes to the tree. The node_list is a block, which is taken to be a hash of node names/node identities and the contents to add to the identified nodes. If the block_name is nil or ROOT, than we add to the root of the tree.

@note: Since the argument list is a {Hash}, each node in the list must be unique. Otherwise later arguments will override earlier ones, and you *will not* get the behaviour you think.

# File lib/data_structures/standard_tree.rb, line 59
def << (node_list)
  Hash[*node_list.flatten].each{|parent, contents|
    add_child(parent, contents)
  }
end
[](block_name) click to toggle source

Look for the designated block within tree starting at the current node. If the block_name is nil or ROOT, than we add to the root of the tree.

# File lib/data_structures/standard_tree.rb, line 67
def [] (block_name)
  return @nodes[block_name]
end
add_child(parent, contents) click to toggle source

Adds a new node to the tree, below the parent node and with the specified contents.

# File lib/data_structures/standard_tree.rb, line 77
def add_child(parent, contents)
  
  # Find the parent node
  if parent.nil? then
    # Add to the root node
    @nodes << StandardNode.new(@next_id.to_s, contents)
  else
    # Add to the relevant node
    @nodes[parent] << StandardNode.new(@next_id.to_s, contents)
  end
  
  # Increment the node index
  @next_id = @next_id.next
  
end
empty?() click to toggle source

Is the tree 'empty' (i.e. it has only the root node)?

# File lib/data_structures/standard_tree.rb, line 98
def empty?
  return !@nodes.has_children?
end