objenealogist 0.1.1 → 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 +32 -59
- 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,12 +28,12 @@ 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
|
|
|
@@ -72,98 +74,69 @@ class Objenealogist
|
|
|
72
74
|
locations = location_map[clazz.superclass.to_s.to_sym]
|
|
73
75
|
result << "#{indent}└── C #{clazz.superclass}#{format_locations(locations, show_locations:,
|
|
74
76
|
target: clazz.superclass.to_s)}"
|
|
75
|
-
if clazz.superclass
|
|
76
|
-
process_one(clazz.superclass, result, location_map, " #{indent}", show_methods:, show_locations:)
|
|
77
|
-
else
|
|
77
|
+
if clazz.superclass == ::BasicObject
|
|
78
78
|
result
|
|
79
|
+
else
|
|
80
|
+
process_one(clazz.superclass, result, location_map, " #{indent}", show_methods:, show_locations:)
|
|
79
81
|
end
|
|
80
82
|
else
|
|
81
83
|
result
|
|
82
84
|
end
|
|
83
85
|
end
|
|
84
86
|
|
|
85
|
-
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
|
|
86
88
|
return "" unless show_locations
|
|
87
89
|
return "" if show_locations.is_a?(Regexp) && show_locations !~ target
|
|
88
90
|
|
|
89
|
-
if locations.is_a?(String)
|
|
91
|
+
if locations.is_a?(::String)
|
|
90
92
|
if locations != ":" && show_locations
|
|
91
93
|
" (location: #{locations})"
|
|
92
94
|
else
|
|
93
95
|
""
|
|
94
96
|
end
|
|
95
|
-
elsif locations
|
|
96
|
-
" (location: #{locations
|
|
97
|
+
elsif locations&.any? && show_locations
|
|
98
|
+
" (location: #{locations.map { |path, loc| "#{path}:#{loc.start_line}" }.join(", ")})"
|
|
97
99
|
else
|
|
98
100
|
""
|
|
99
101
|
end
|
|
100
102
|
end
|
|
101
103
|
|
|
102
|
-
def create_location_map(clazz)
|
|
104
|
+
def create_location_map(clazz) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
|
103
105
|
instance = clazz.allocate
|
|
104
106
|
methods = clazz.methods + instance.methods
|
|
105
|
-
|
|
107
|
+
location_map = {}
|
|
108
|
+
methods.map do |m|
|
|
106
109
|
if clazz.respond_to?(m) && clazz.method(m).source_location
|
|
107
|
-
|
|
110
|
+
clazz.method(m).source_location[0]
|
|
108
111
|
elsif instance.respond_to?(m) && instance.method(m).source_location
|
|
109
|
-
|
|
112
|
+
instance.method(m).source_location[0]
|
|
110
113
|
end
|
|
111
|
-
end.compact
|
|
112
|
-
location_map = {}
|
|
113
|
-
source_locations.uniq { |_, path,| path }.each do |method, path, line|
|
|
114
|
+
end.compact.uniq.each do |path| # rubocop:disable Style/MultilineBlockChain
|
|
114
115
|
next unless File.exist?(path)
|
|
115
116
|
|
|
116
|
-
source = File.
|
|
117
|
+
source = File.read(path)
|
|
117
118
|
visitor = ClassVisitor.new(clazz.ancestors)
|
|
118
119
|
Prism.parse(source).value.accept(visitor)
|
|
119
120
|
visitor.found.each do |name, def_location|
|
|
120
|
-
(location_map[name] ||=
|
|
121
|
+
(location_map[name] ||= []) << [path, def_location]
|
|
121
122
|
end
|
|
122
123
|
end
|
|
123
|
-
# {M1:
|
|
124
|
-
# {name: :M1,
|
|
125
|
-
# locations: [["objenealogist.rb", (122,0)-(124,3)]]}
|
|
124
|
+
# {M1: [["objenealogist.rb", (122,0)-(124,3)]]}
|
|
126
125
|
location_map
|
|
127
126
|
end
|
|
128
127
|
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
module M1
|
|
132
|
-
def m1 = :m1
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
module M2
|
|
136
|
-
def m2 = :m2
|
|
137
|
-
end
|
|
138
|
-
|
|
139
|
-
module M3
|
|
140
|
-
def m3 = :m3
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
module M4
|
|
144
|
-
include M3
|
|
145
|
-
def m4 = :m4
|
|
146
|
-
end
|
|
147
128
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
class C1
|
|
153
|
-
include M5
|
|
154
|
-
def c1 = :c1
|
|
155
|
-
end
|
|
129
|
+
class String < ::String # rubocop:disable Style/Documentation
|
|
130
|
+
def >>(other)
|
|
131
|
+
raise "A file already exists!" if File.exist?(other)
|
|
156
132
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
include M4
|
|
160
|
-
def c2 = :c2
|
|
133
|
+
File.write(other, to_s)
|
|
134
|
+
end
|
|
161
135
|
end
|
|
162
136
|
end
|
|
163
137
|
|
|
164
|
-
class
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
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
|
|
169
142
|
end
|