module TreeView

Constants

ARRAY_CLASS
HASH_CLASS
VERSION

Public Class Methods

finally(tree) click to toggle source
# File lib/treeview.rb, line 15
def self.finally(tree)
  tree = tree.split("\n").map!(&:chars)
  (0..(tree.length-1)).each do |y|
    next if y == tree.length
    tree[y].each.with_index do |ch,i|
      if ch == '|' && tree[y+1][i] == ' '
        tree[y+1][i] = '|'
      end
    end
  end
  tree.map!{|e|e.join('')}
  tree.join("\n")
end
parse(source,nest = 0) click to toggle source
# File lib/treeview.rb, line 29
def self.parse(source,nest = 0)
  str = ''

  if [HASH_CLASS,ARRAY_CLASS].include?(source.class)
    len = source.length
  end

  if source.class == HASH_CLASS
    source.each.with_index(1) do |(k,v) ,i|
      str << k.to_s + "\n"
      str << parse(v,nest)
    end
  elsif source.class == ARRAY_CLASS
    nest += 1
    source.each.with_index(1) do |v,i|
      str << write_branch(i,len,nest)
      str << parse(v, nest)
    end
  else
    str << source.to_s + "\n"
  end
  str
end
tree(source) click to toggle source
# File lib/treeview.rb, line 53
def self.tree(source)
  finally(parse(source))
end
write_branch(pos,last,nest) click to toggle source
# File lib/treeview.rb, line 11
def self.write_branch(pos,last,nest)
  @@SPACE * (nest - 1) + (pos != last ? @@BRANCH : @@END_BRANCH)
end