Class: Treat::Workers::Formatters::Visualizers::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/treat/workers/formatters/visualizers/tree.rb

Overview

Visualization of entities in ASCII tree format.

Constant Summary collapse

DefaultOptions =

Start out with an indent at 0.

{ indent: 0 }

Class Method Summary collapse

Class Method Details

.visualize(entity, options = {}) ⇒ Object

Obtain a plain text tree representation of the entity.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/treat/workers/formatters/visualizers/tree.rb', line 9

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