Sha256: 46f45f22d0cc67d5c09d9b27d05079bda1f3d3ab92fceadad0851bad81c5ba65

Contents?: true

Size: 915 Bytes

Versions: 3

Compression:

Stored size: 915 Bytes

Contents

# frozen_string_literal: true

#
# Copyright (c) 2018-present, Blue Marble Payroll, LLC
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#

module TreeBranch
  # Main class the outlines the basic operations and structure of a node in the tree.
  class Node
    attr_reader :data, :children

    def initialize(data)
      @data     = data
      @children = []
    end

    def add(*children_to_add)
      children_to_add.flatten.each do |child|
        raise ArgumentError, "Improper class: #{child.class.name}" unless child.is_a?(self.class)

        @children << child
      end

      self
    end

    def eql?(other)
      data == other.data && children == other.children
    end

    def ==(other)
      eql?(other)
    end

    def to_s
      "[#{self.class.name}] Data: #{data}, Child Count: #{children.length}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tree_branch-1.1.1 lib/tree_branch/node.rb
tree_branch-1.1.0 lib/tree_branch/node.rb
tree_branch-1.0.0 lib/tree_branch/node.rb