Sha256: 081a772c428a5282b00070d1e4c65c8821daa01afbb173e0113affcce73a7bdc

Contents?: true

Size: 806 Bytes

Versions: 5

Compression:

Stored size: 806 Bytes

Contents

module Vedeu

  # A composition class.
  #
  class Node

    attr_accessor :name,
      :parent

    attr_reader   :children

    # @return [void]
    def initialize(name)
      @name     = name
      @children = []
    end

    # @return [void]
    def add_child(node)
      children << node

      node.parent = self
    end
    alias :<< :add_child

    # @return [void]
    def remove_child(node)
      children.delete(node)
    end

    # @return [void]
    def [](index)
      children[index]
    end

    # @return [void]
    def []=(index, node)
      replaced_child        = @children[index]
      children[index]       = node
      replaced_child.parent = nil
      node.parent           = self
    end

    # @return [void]
    def leaf?
      children.empty?
    end

  end # Node

end # Vedeu

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.4.3 lib/vedeu/support/node.rb
vedeu-0.4.2 lib/vedeu/support/node.rb
vedeu-0.4.1 lib/vedeu/support/node.rb
vedeu-0.4.0 lib/vedeu/support/node.rb
vedeu-0.3.5 lib/vedeu/support/node.rb