Sha256: 291bac964d82691b619ec893446762a63932070e2ffebf56c69ce088310d4be0

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

# Copyright (c) 2010-2011 David Love
#
# Permission to use, copy, modify, and/or distribute this software for 
# any purpose with or without fee is hereby granted, provided that the 
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# @author David Love

module WhiteCloth::DataStructures
  
  # Load the base tree data structure
  require "tree"
    
  class StandardNode < Tree::TreeNode
    
    ###
    ### Constructors
    ###
    
    def initialize(node_id, content)
      super(node_id, content)
    end
    
    ###
    ### Operators
    ###
    
    # Add a new child node to the current node.
    def << (child_node)
      self.add(child_node)
    end
    
    # Look for the designated block within tree starting at the current node. If the +block_name+
    # is +nil+ or +ROOT+, than we return the root of the current tree (i.e. ourselves).
    def [] (block_name)
      
      # We need to work out which node has the right content. Since
      # the nodes are effectively unordered, we have to look (potentially)
      # at every node
      unless block_name.nil? or block_name == "ROOT"
        self.each{|child|
          if child.content === block_name then
            return child
          end
        }
        return nil
      else
        return self
      end      
    end
    
    ###
    ### Accessors
    ###
    
    # State that +id+ is an alias for the node name.
    alias :id :name
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
WhiteCloth-0.0.5 lib/data_structures/standard_node.rb