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.



115
116
117
118
# File 'lib/gitingest/generator.rb', line 115

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

Instance Method Details

#buildObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gitingest/generator.rb', line 120

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