Class: ObjectTree
- Inherits:
-
Object
- Object
- ObjectTree
- Defined in:
- lib/object_tree.rb,
lib/object_tree/version.rb
Constant Summary collapse
- OPTIONS =
{ color: true, exclude: [], space_size: 8 }
- SPACE_SIZE =
8
- T_LINE =
'├─────'
- I_LINE =
'│'
- L_LINE =
'└─────'
- VERSION =
"1.1.0"
Instance Attribute Summary collapse
-
#tree ⇒ Object
readonly
Returns the value of attribute tree.
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
Returns a new instance of ObjectTree.
16 17 18 19 20 |
# File 'lib/object_tree.rb', line 16 def initialize(klass) @tree = Hash.new {|h,k| h[k] = [] } @queue = [] parse(klass) end |
Instance Attribute Details
#tree ⇒ Object (readonly)
Returns the value of attribute tree.
4 5 6 |
# File 'lib/object_tree.rb', line 4 def tree @tree end |
Class Method Details
.create(klass) ⇒ Object
12 13 14 |
# File 'lib/object_tree.rb', line 12 def self.create(klass) new(klass) end |
Instance Method Details
#get_line(end_line: nil, space: '') ⇒ Object
34 35 36 |
# 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) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/object_tree.rb', line 55 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) ⇒ Object
38 39 40 |
# 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) ⇒ Object
26 27 28 29 30 31 32 |
# 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: []) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/object_tree.rb', line 42 def parse(klass, space = '', path: []) path << klass 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.to_s @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
22 23 24 |
# File 'lib/object_tree.rb', line 22 def to_s OPTIONS[:color] ? @queue.join : @queue.join.uncolorize end |