class Dirtree::Node

tree node, it has a name and children of the same type

Attributes

children[R]
name[R]

Public Class Methods

new(name) click to toggle source
# File lib/dirtree/node.rb, line 6
def initialize(name)
  @name = name
  @children = []
end

Public Instance Methods

as_json() click to toggle source
# File lib/dirtree/node.rb, line 27
def as_json
  {
    name: name,
    children: children.map(&:as_json)
  }
end
insert(path) click to toggle source

insert a node by its path, path is an array of names (Strings)

# File lib/dirtree/node.rb, line 12
def insert(path)
  return if path.empty?

  child_name = path.first
  grandchildren = path[1..-1]

  child = @children.find { |current| current.name == child_name }
  unless child
    child = Node.new(child_name)
    @children << child
  end

  child.insert(grandchildren)
end