objenealogist 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +50 -18
- data/lib/objenealogist/version.rb +1 -1
- data/lib/objenealogist.rb +57 -88
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3bf00f425eca6902d2ab65a19c0b125f6a3a7462534f7aca0451097d6598f71e
|
|
4
|
+
data.tar.gz: 6c70cd4562686b12755f94bc0c4d40f702a6ee787eecc3b73c051a8b4a653bfd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 200cb4292920be6f24f1d15cb18b6e4eeab114534e9654968fc22c5892aefc5ba2704ff64534897928850f24d980be2615e710860130c0f4001b586887801852
|
|
7
|
+
data.tar.gz: f65b8678119db27c0b9747ac2b9a2df749da48e3701516db6a9ec5cafd6bca1633ab2789199198491d45912f3e3f5e9d6c41c2080a573dedfbece00ac63e1220
|
data/README.md
CHANGED
|
@@ -1,39 +1,71 @@
|
|
|
1
1
|
# Objenealogist
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> object + genealogist -> objenealogist
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A Ruby gem that visualizes class inheritance hierarchy and included modules in a tree format.
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
-
|
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
|
12
|
-
|
|
13
9
|
```bash
|
|
14
|
-
|
|
10
|
+
gem install objenealogist
|
|
15
11
|
```
|
|
16
12
|
|
|
17
|
-
|
|
13
|
+
## Usage
|
|
18
14
|
|
|
19
|
-
```
|
|
20
|
-
|
|
15
|
+
```ruby
|
|
16
|
+
require 'objenealogist'
|
|
17
|
+
|
|
18
|
+
puts MyClass.to_tree
|
|
21
19
|
```
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
### Options
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
- `show_methods`: Show public methods for each class/module (default: `true`)
|
|
24
|
+
- `show_locations`: Show source file locations (default: `true`). You can also pass a `Regexp` to show locations only for matching class/module names.
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
### Example
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
```ruby
|
|
29
|
+
MyClass.to_tree(show_locations: false)
|
|
30
|
+
```
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
```
|
|
33
|
+
C MyClass
|
|
34
|
+
│ ├ c
|
|
35
|
+
│ └ singleton_c
|
|
36
|
+
│
|
|
37
|
+
├── M M2
|
|
38
|
+
│ └ m2
|
|
39
|
+
│
|
|
40
|
+
├── M M1
|
|
41
|
+
│ └ m1
|
|
42
|
+
│
|
|
43
|
+
└── C NS::C2
|
|
44
|
+
│ └ c2
|
|
45
|
+
│
|
|
46
|
+
├── M M4
|
|
47
|
+
│ └ m4
|
|
48
|
+
│
|
|
49
|
+
├── M M3
|
|
50
|
+
│ └ m3
|
|
51
|
+
│
|
|
52
|
+
└── C C1
|
|
53
|
+
│ └ c1
|
|
54
|
+
│
|
|
55
|
+
├── M M5
|
|
56
|
+
│ └ m5
|
|
57
|
+
│
|
|
58
|
+
└── C Object
|
|
59
|
+
├── M Kernel
|
|
60
|
+
└── C BasicObject
|
|
61
|
+
```
|
|
32
62
|
|
|
33
|
-
|
|
63
|
+
### Output to file
|
|
34
64
|
|
|
35
|
-
|
|
65
|
+
```ruby
|
|
66
|
+
MyClass.to_tree >> "out.txt"
|
|
67
|
+
```
|
|
36
68
|
|
|
37
69
|
## License
|
|
38
70
|
|
|
39
|
-
|
|
71
|
+
MIT License
|
data/lib/objenealogist.rb
CHANGED
|
@@ -4,7 +4,8 @@ require "prism"
|
|
|
4
4
|
|
|
5
5
|
require_relative "objenealogist/version"
|
|
6
6
|
|
|
7
|
-
class Objenealogist
|
|
7
|
+
class Objenealogist # rubocop:disable Style/Documentation
|
|
8
|
+
# `class` and `module` node visitor
|
|
8
9
|
class ClassVisitor < Prism::Visitor
|
|
9
10
|
attr_reader :found
|
|
10
11
|
|
|
@@ -12,6 +13,7 @@ class Objenealogist
|
|
|
12
13
|
@target_class_names = target_class_names.map(&:to_s).map(&:to_sym)
|
|
13
14
|
@found = []
|
|
14
15
|
@stack = []
|
|
16
|
+
super()
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def visit_class_node(node)
|
|
@@ -26,40 +28,45 @@ class Objenealogist
|
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
class << self
|
|
29
|
-
def to_tree(clazz, show_methods: true, show_locations: true)
|
|
30
|
-
# ruby -r./lib/objenealogist -e
|
|
31
|
-
process_one(clazz, show_methods:, show_locations:).join("\n")
|
|
31
|
+
def to_tree(clazz = self, show_methods: true, show_locations: true)
|
|
32
|
+
# ruby -r./lib/objenealogist -r./test/my_class -e "puts Objenealogist.to_tree(MyClass)"
|
|
33
|
+
Objenealogist::String.new(process_one(clazz, show_methods:, show_locations:).join("\n"))
|
|
32
34
|
end
|
|
33
35
|
|
|
34
|
-
def process_one(clazz, result = [], location_map = create_location_map(clazz), indent = "", show_methods: true,
|
|
36
|
+
def process_one(clazz, result = [], location_map = create_location_map(clazz), indent = "", show_methods: true, # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity,Metrics/ParameterLists
|
|
35
37
|
show_locations: true)
|
|
36
38
|
locations = location_map[clazz.to_s.to_sym]
|
|
37
39
|
|
|
38
40
|
result << "#{indent}C #{clazz}#{format_locations(locations, show_locations:, target: clazz.to_s)}" if indent == ""
|
|
39
|
-
if
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
m, path, line = method
|
|
44
|
-
mark = locations[:methods].size - 1 == index ? "└" : "├"
|
|
45
|
-
result << "#{indent}│ #{mark} #{m}#{format_locations("#{path}:#{line}", show_locations:, target: clazz.to_s)}"
|
|
41
|
+
if show_methods
|
|
42
|
+
methods = class << clazz; public_instance_methods(false).map { |m| instance_method(m) }; end
|
|
43
|
+
methods += clazz.public_instance_methods(false).map do |m|
|
|
44
|
+
clazz.instance_method(m)
|
|
46
45
|
end
|
|
47
|
-
|
|
46
|
+
methods.compact.sort { |a, b| a.name <=> b.name }.each_with_index do |method, index|
|
|
47
|
+
path, line = method.source_location
|
|
48
|
+
mark = methods.size - 1 == index ? "└" : "├"
|
|
49
|
+
result << "#{indent}│ #{mark} #{method.name}#{format_locations("#{path}:#{line}", show_locations:,
|
|
50
|
+
target: clazz.to_s)}"
|
|
51
|
+
end
|
|
52
|
+
result << "#{indent}│" if methods.any?
|
|
48
53
|
end
|
|
49
54
|
|
|
50
55
|
(clazz.included_modules - (clazz.superclass&.included_modules || [])).each do |mod|
|
|
51
56
|
locations = location_map[mod.to_s.to_sym]
|
|
52
57
|
result << "#{indent}├── M #{mod}#{format_locations(locations, show_locations:, target: mod.to_s)}"
|
|
53
|
-
if
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
if show_methods && mod != ::Kernel # rubocop:disable Style/Next
|
|
59
|
+
methods = class << mod; public_instance_methods(false).map { |m| instance_method(m) }; end
|
|
60
|
+
methods += mod.public_instance_methods(false).map do |m|
|
|
61
|
+
clazz.instance_method(m)
|
|
62
|
+
end
|
|
63
|
+
methods.compact.sort { |a, b| a.name <=> b.name }.each_with_index do |method, index|
|
|
64
|
+
path, line = method.source_location
|
|
65
|
+
mark = methods.size - 1 == index ? "└" : "├"
|
|
66
|
+
result << "#{indent}│ #{mark} #{method.name}#{format_locations("#{path}:#{line}", show_locations:,
|
|
67
|
+
target: mod.to_s)}"
|
|
61
68
|
end
|
|
62
|
-
result << "#{indent}
|
|
69
|
+
result << "#{indent}│" if methods.any?
|
|
63
70
|
end
|
|
64
71
|
end
|
|
65
72
|
|
|
@@ -67,107 +74,69 @@ class Objenealogist
|
|
|
67
74
|
locations = location_map[clazz.superclass.to_s.to_sym]
|
|
68
75
|
result << "#{indent}└── C #{clazz.superclass}#{format_locations(locations, show_locations:,
|
|
69
76
|
target: clazz.superclass.to_s)}"
|
|
70
|
-
|
|
77
|
+
if clazz.superclass == ::BasicObject
|
|
78
|
+
result
|
|
79
|
+
else
|
|
80
|
+
process_one(clazz.superclass, result, location_map, " #{indent}", show_methods:, show_locations:)
|
|
81
|
+
end
|
|
71
82
|
else
|
|
72
83
|
result
|
|
73
84
|
end
|
|
74
85
|
end
|
|
75
86
|
|
|
76
|
-
def format_locations(locations, show_locations: true, target: "")
|
|
87
|
+
def format_locations(locations, show_locations: true, target: "") # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
|
77
88
|
return "" unless show_locations
|
|
78
89
|
return "" if show_locations.is_a?(Regexp) && show_locations !~ target
|
|
79
90
|
|
|
80
|
-
if locations.is_a?(String)
|
|
81
|
-
if locations != ":
|
|
91
|
+
if locations.is_a?(::String)
|
|
92
|
+
if locations != ":" && show_locations
|
|
82
93
|
" (location: #{locations})"
|
|
83
94
|
else
|
|
84
95
|
""
|
|
85
96
|
end
|
|
86
|
-
elsif locations
|
|
87
|
-
" (location: #{locations
|
|
97
|
+
elsif locations&.any? && show_locations
|
|
98
|
+
" (location: #{locations.map { |path, loc| "#{path}:#{loc.start_line}" }.join(", ")})"
|
|
88
99
|
else
|
|
89
100
|
""
|
|
90
101
|
end
|
|
91
102
|
end
|
|
92
103
|
|
|
93
|
-
def create_location_map(clazz)
|
|
104
|
+
def create_location_map(clazz) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
|
94
105
|
instance = clazz.allocate
|
|
95
106
|
methods = clazz.methods + instance.methods
|
|
96
|
-
|
|
107
|
+
location_map = {}
|
|
108
|
+
methods.map do |m|
|
|
97
109
|
if clazz.respond_to?(m) && clazz.method(m).source_location
|
|
98
|
-
|
|
110
|
+
clazz.method(m).source_location[0]
|
|
99
111
|
elsif instance.respond_to?(m) && instance.method(m).source_location
|
|
100
|
-
|
|
112
|
+
instance.method(m).source_location[0]
|
|
101
113
|
end
|
|
102
|
-
end.compact
|
|
103
|
-
location_map = {}
|
|
104
|
-
source_locations.uniq { |_, path,| path }.each do |method, path, line|
|
|
114
|
+
end.compact.uniq.each do |path| # rubocop:disable Style/MultilineBlockChain
|
|
105
115
|
next unless File.exist?(path)
|
|
106
116
|
|
|
107
|
-
source = File.
|
|
117
|
+
source = File.read(path)
|
|
108
118
|
visitor = ClassVisitor.new(clazz.ancestors)
|
|
109
119
|
Prism.parse(source).value.accept(visitor)
|
|
110
120
|
visitor.found.each do |name, def_location|
|
|
111
|
-
(location_map[name] ||=
|
|
121
|
+
(location_map[name] ||= []) << [path, def_location]
|
|
112
122
|
end
|
|
113
123
|
end
|
|
114
|
-
|
|
115
|
-
locations = location_map.values.find do |location|
|
|
116
|
-
location[:locations].any? do |source_path, loc|
|
|
117
|
-
method_path == source_path && loc.start_line <= line && line <= loc.end_line
|
|
118
|
-
end
|
|
119
|
-
end
|
|
120
|
-
if locations
|
|
121
|
-
locations[:methods] << [m, method_path, line]
|
|
122
|
-
else
|
|
123
|
-
location_map[clazz.to_s.to_sym]&.[](:methods)&.<< [m, nil, 0]
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
# {M1:
|
|
127
|
-
# {name: :M1,
|
|
128
|
-
# locations: [["objenealogist.rb", (122,0)-(124,3)]],
|
|
129
|
-
# methods: [[:m1, "objenealogist.rb", 123]]}}
|
|
124
|
+
# {M1: [["objenealogist.rb", (122,0)-(124,3)]]}
|
|
130
125
|
location_map
|
|
131
126
|
end
|
|
132
127
|
end
|
|
133
|
-
end
|
|
134
128
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
129
|
+
class String < ::String # rubocop:disable Style/Documentation
|
|
130
|
+
def >>(other)
|
|
131
|
+
raise "A file already exists!" if File.exist?(other)
|
|
138
132
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
module M3
|
|
144
|
-
def m3 = :m3
|
|
145
|
-
end
|
|
146
|
-
|
|
147
|
-
module M4
|
|
148
|
-
include M3
|
|
149
|
-
def m4 = :m4
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
module M5
|
|
153
|
-
def m5 = :m5
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
class C1
|
|
157
|
-
include M5
|
|
158
|
-
def c1 = :c1
|
|
159
|
-
end
|
|
160
|
-
|
|
161
|
-
module NS
|
|
162
|
-
class C2 < C1
|
|
163
|
-
include M4
|
|
164
|
-
def c2 = :c2
|
|
133
|
+
File.write(other, to_s)
|
|
134
|
+
end
|
|
165
135
|
end
|
|
166
136
|
end
|
|
167
137
|
|
|
168
|
-
class
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
def self.singleton_c = :singleton_c
|
|
138
|
+
class Class # rubocop:disable Style/Documentation
|
|
139
|
+
def to_tree(show_methods: true, show_locations: true)
|
|
140
|
+
Objenealogist.to_tree(self, show_methods:, show_locations:)
|
|
141
|
+
end
|
|
173
142
|
end
|