Class: ObjectTree
- Inherits:
-
Object
- Object
- ObjectTree
- Defined in:
- lib/object_tree.rb,
lib/object_tree/version.rb
Constant Summary collapse
- SPACE_SIZE =
8- T_LINE =
'├─────'- I_LINE =
'│'- L_LINE =
'└─────'- VERSION =
"1.0.0"
Class Method Summary collapse
Instance Method Summary collapse
- #get_line(end_line: nil, space: '') ⇒ Object
- #get_modules(klass, path) ⇒ Object
- #get_space(end_line: nil) ⇒ Object
-
#initialize(klass) ⇒ ObjectTree
constructor
A new instance of ObjectTree.
- #output_node(klass) ⇒ Object
- #parse(klass, space = '', path: []) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(klass) ⇒ ObjectTree
13 14 15 16 |
# File 'lib/object_tree.rb', line 13 def initialize(klass) @queue = [] parse(klass) end |
Class Method Details
.create(klass) ⇒ Object
9 10 11 |
# File 'lib/object_tree.rb', line 9 def self.create(klass) new(klass) end |
Instance Method Details
#get_line(end_line: nil, space: '') ⇒ Object
30 31 32 |
# File 'lib/object_tree.rb', line 30 def get_line(end_line: nil, space: '') end_line ? "#{space}#{L_LINE} " : "#{space}#{T_LINE} " end |
#get_modules(klass, path) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/object_tree.rb', line 51 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 end |
#get_space(end_line: nil) ⇒ Object
34 35 36 |
# File 'lib/object_tree.rb', line 34 def get_space(end_line: nil) end_line ? ' ' * SPACE_SIZE : I_LINE + ' ' * (SPACE_SIZE-1) end |
#output_node(klass) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/object_tree.rb', line 22 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: []) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/object_tree.rb', line 38 def parse(klass, space = '', path: []) path << klass modules = get_modules(klass, path.reverse) @queue << output_node(klass) until modules.empty? sub = modules.shift @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 ⇒ Object
18 19 20 |
# File 'lib/object_tree.rb', line 18 def to_s @queue.join end |