louismullie/treat

View on GitHub
lib/treat/workers/formatters/visualizers/tree.rb

Summary

Maintainability
A
0 mins
Test Coverage
# Visualization of entities in ASCII tree format.
class Treat::Workers::Formatters::Visualizers::Tree

  # Start out with an indent at 0.
  DefaultOptions = { indent: 0 }
  
  # Obtain a plain text tree representation
  # of the entity.
  def self.visualize(entity, options = {})
    options = DefaultOptions.merge(options)
    string = ''
    if entity.has_children?
      spacer = '--'
      spaces = ''
      options[:indent].times { spaces << '   '}
      string << "+ #{entity.inspect}\n#{spaces}|"
      options[:indent] += 1
      entity.children.each do |child|
        string = string + "\n" + spaces + '+' +
        spacer + self.visualize(child, options)
      end
      options[:indent] -= 1
      return string
    end
    '> ' + entity.inspect
  end
  
end