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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca90d0b5901aa3b2d3d517d772c715b926545530421ea695646a80f4c10e432c
4
- data.tar.gz: 7f4f3f5a7aa8b5b1f24855e7b8fa68c258dbbc42686ff29ddc5767105d35782d
3
+ metadata.gz: 3bf00f425eca6902d2ab65a19c0b125f6a3a7462534f7aca0451097d6598f71e
4
+ data.tar.gz: 6c70cd4562686b12755f94bc0c4d40f702a6ee787eecc3b73c051a8b4a653bfd
5
5
  SHA512:
6
- metadata.gz: e406e9f86b6708851d5d56a2340001ec1d6574a8f36481bf1b388103f14712d24f38886d12529860d3b5f84b7673621b78d945f8a93a10d3cd24285e9673c586
7
- data.tar.gz: 9718d6c0482ee0e89799c75afb6d86a6e4b90824cae2ff09033da119e0dae326db491b4d9c5cf2fa6cce0620403453302c430c4a6cc60b6cfd92f5ebdfa0231e
6
+ metadata.gz: 200cb4292920be6f24f1d15cb18b6e4eeab114534e9654968fc22c5892aefc5ba2704ff64534897928850f24d980be2615e710860130c0f4001b586887801852
7
+ data.tar.gz: f65b8678119db27c0b9747ac2b9a2df749da48e3701516db6a9ec5cafd6bca1633ab2789199198491d45912f3e3f5e9d6c41c2080a573dedfbece00ac63e1220
data/README.md CHANGED
@@ -1,39 +1,71 @@
1
1
  # Objenealogist
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ > object + genealogist -> objenealogist
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/objenealogist`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ gem install objenealogist
15
11
  ```
16
12
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
13
+ ## Usage
18
14
 
19
- ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```ruby
16
+ require 'objenealogist'
17
+
18
+ puts MyClass.to_tree
21
19
  ```
22
20
 
23
- ## Usage
21
+ ### Options
24
22
 
25
- TODO: Write usage instructions here
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
- ## Development
26
+ ### Example
28
27
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+ ```ruby
29
+ MyClass.to_tree(show_locations: false)
30
+ ```
30
31
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
63
+ ### Output to file
34
64
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/objenealogist.
65
+ ```ruby
66
+ MyClass.to_tree >> "out.txt"
67
+ ```
36
68
 
37
69
  ## License
38
70
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
71
+ MIT License
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Objenealogist
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
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 'puts Objenealogist.to_tree(Objenealogist)'
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 locations && locations[:methods] && show_methods
40
- locations[:methods].sort do |a, b|
41
- a[2] == b[2] ? a[0] <=> b[0] : a[2] <=> b[2]
42
- end.each_with_index do |method, index|
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
- result << "#{indent}│"
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 locations && locations[:methods] && show_methods # rubocop:disable Style/Next
54
- locations[:methods].sort do |a, b|
55
- a[2] == b[2] ? a[0] <=> b[0] : a[2] <=> b[2]
56
- end.each_with_index do |method, index|
57
- m, path, line = method
58
- mark = locations[:methods].size - 1 == index ? "└" : "├"
59
- result << "#{indent}| #{mark} #{m}#{format_locations("#{path}:#{line}", show_locations:,
60
- target: mod.to_s)}"
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
- process_one(clazz.superclass, result, location_map, " #{indent}", show_methods:, show_locations:)
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 != ":0" && show_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 && locations[:locations]&.any? && show_locations
87
- " (location: #{locations[:locations].map { |path, loc| "#{path}:#{loc.start_line}" }.join(", ")})"
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
- source_locations = methods.map do |m|
107
+ location_map = {}
108
+ methods.map do |m|
97
109
  if clazz.respond_to?(m) && clazz.method(m).source_location
98
- [m] + clazz.method(m).source_location
110
+ clazz.method(m).source_location[0]
99
111
  elsif instance.respond_to?(m) && instance.method(m).source_location
100
- [m] + instance.method(m).source_location
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.open(path).read
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] ||= { name: name, locations: [], methods: [] })[:locations] << [path, def_location]
121
+ (location_map[name] ||= []) << [path, def_location]
112
122
  end
113
123
  end
114
- source_locations.uniq(&:join).each do |m, method_path, line|
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
- module M1
136
- def m1 = :m1
137
- end
129
+ class String < ::String # rubocop:disable Style/Documentation
130
+ def >>(other)
131
+ raise "A file already exists!" if File.exist?(other)
138
132
 
139
- module M2
140
- def m2 = :m2
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 MyClass < NS::C2
169
- include M1
170
- include M2
171
- def c = :c
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: objenealogist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji NAKAMURA