Class: Gitingest::DirectoryStructureBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gitingest/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_name, files) ⇒ DirectoryStructureBuilder

Returns a new instance of DirectoryStructureBuilder.



411
412
413
414
# File 'lib/gitingest/generator.rb', line 411

def initialize(root_name, files)
  @root_name = root_name
  @files = files.map(&:path)
end

Instance Method Details

#buildObject



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/gitingest/generator.rb', line 416

def build
  tree = { @root_name => {} }
  @files.sort.each do |path|
    parts = path.split("/")
    current = tree[@root_name]
    parts.each do |part|
      if part == parts.last then current[part] = nil
      else
        current[part] ||= {}
        current = current[part]
      end
    end
  end
  output = ["Directory structure:"]
  render_tree(tree, "", output)
  output.join("\n")
end