class ObjectTree

Constants

I_LINE
L_LINE
OPTIONS
SPACE_SIZE
T_LINE
VERSION

Attributes

tree[R]

Public Class Methods

create(klass) click to toggle source
# File lib/object_tree.rb, line 12
def self.create(klass)
  new(klass)
end
new(klass) click to toggle source
# File lib/object_tree.rb, line 16
def initialize(klass)
  @tree = {}
  @queue = []
  parse(klass)
end

Public Instance Methods

get_line(end_line: nil, space: '') click to toggle source
# File lib/object_tree.rb, line 34
def get_line(end_line: nil, space: '')
  end_line ? "#{space}#{L_LINE} " : "#{space}#{T_LINE} "
end
get_modules(klass, path) click to toggle source
# File lib/object_tree.rb, line 56
def get_modules(klass, path)
  ObjectSpace.each_object(Module).map do |k|
    l = k.ancestors
    if l.each_cons(path.size).include?(path)
      (l[l.index(k)..l.index(klass)] - path).last
    end
  end.compact.uniq.sort_by(&:to_s)
end
get_space(end_line: nil) click to toggle source
# File lib/object_tree.rb, line 38
def get_space(end_line: nil)
  end_line ? ' ' * OPTIONS[:space_size] : I_LINE + ' ' * (OPTIONS[:space_size]-1)
end
output_node(klass) click to toggle source
# File lib/object_tree.rb, line 26
def output_node(klass)
  if klass.instance_of?(Class)
    "<#{?C.colorize(:green)}> #{klass}\n"
  else
    "<#{?M.colorize(:red)}> #{klass}\n"
  end
end
parse(klass, space = '', path: []) click to toggle source
# File lib/object_tree.rb, line 42
def parse(klass, space = '', path: [])
  path << klass
  @tree[path.join('/')] = []
  modules = get_modules(klass, path.reverse)
  @queue << output_node(klass)

  while sub = modules.shift
    next if OPTIONS[:exclude].include?(sub.to_s)
    @tree[path.join('/')] << sub
    @queue << get_line(end_line: modules.empty?, space: space)
    parse(sub, space + get_space(end_line: modules.empty?), path: path.dup)
  end
end
to_s() click to toggle source
# File lib/object_tree.rb, line 22
def to_s
  OPTIONS[:color] ? @queue.join : @queue.join.uncolorize
end