objenealogist 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ca90d0b5901aa3b2d3d517d772c715b926545530421ea695646a80f4c10e432c
4
+ data.tar.gz: 7f4f3f5a7aa8b5b1f24855e7b8fa68c258dbbc42686ff29ddc5767105d35782d
5
+ SHA512:
6
+ metadata.gz: e406e9f86b6708851d5d56a2340001ec1d6574a8f36481bf1b388103f14712d24f38886d12529860d3b5f84b7673621b78d945f8a93a10d3cd24285e9673c586
7
+ data.tar.gz: 9718d6c0482ee0e89799c75afb6d86a6e4b90824cae2ff09033da119e0dae326db491b4d9c5cf2fa6cce0620403453302c430c4a6cc60b6cfd92f5ebdfa0231e
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Koji NAKAMURA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Objenealogist
2
+
3
+ TODO: Delete this and the text below, and describe your gem
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.
6
+
7
+ ## Installation
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
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/objenealogist.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Objenealogist
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "prism"
4
+
5
+ require_relative "objenealogist/version"
6
+
7
+ class Objenealogist
8
+ class ClassVisitor < Prism::Visitor
9
+ attr_reader :found
10
+
11
+ def initialize(target_class_names)
12
+ @target_class_names = target_class_names.map(&:to_s).map(&:to_sym)
13
+ @found = []
14
+ @stack = []
15
+ end
16
+
17
+ def visit_class_node(node)
18
+ @stack << node.name
19
+ name = @stack.join("::").to_sym
20
+ @found << [name, node.location] if @target_class_names.include?(name)
21
+ super
22
+ @stack.pop
23
+ end
24
+
25
+ alias visit_module_node visit_class_node
26
+ end
27
+
28
+ 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")
32
+ end
33
+
34
+ def process_one(clazz, result = [], location_map = create_location_map(clazz), indent = "", show_methods: true,
35
+ show_locations: true)
36
+ locations = location_map[clazz.to_s.to_sym]
37
+
38
+ 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)}"
46
+ end
47
+ result << "#{indent}│"
48
+ end
49
+
50
+ (clazz.included_modules - (clazz.superclass&.included_modules || [])).each do |mod|
51
+ locations = location_map[mod.to_s.to_sym]
52
+ 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)}"
61
+ end
62
+ result << "#{indent}|"
63
+ end
64
+ end
65
+
66
+ if clazz.superclass
67
+ locations = location_map[clazz.superclass.to_s.to_sym]
68
+ result << "#{indent}└── C #{clazz.superclass}#{format_locations(locations, show_locations:,
69
+ target: clazz.superclass.to_s)}"
70
+ process_one(clazz.superclass, result, location_map, " #{indent}", show_methods:, show_locations:)
71
+ else
72
+ result
73
+ end
74
+ end
75
+
76
+ def format_locations(locations, show_locations: true, target: "")
77
+ return "" unless show_locations
78
+ return "" if show_locations.is_a?(Regexp) && show_locations !~ target
79
+
80
+ if locations.is_a?(String)
81
+ if locations != ":0" && show_locations
82
+ " (location: #{locations})"
83
+ else
84
+ ""
85
+ end
86
+ elsif locations && locations[:locations]&.any? && show_locations
87
+ " (location: #{locations[:locations].map { |path, loc| "#{path}:#{loc.start_line}" }.join(", ")})"
88
+ else
89
+ ""
90
+ end
91
+ end
92
+
93
+ def create_location_map(clazz)
94
+ instance = clazz.allocate
95
+ methods = clazz.methods + instance.methods
96
+ source_locations = methods.map do |m|
97
+ if clazz.respond_to?(m) && clazz.method(m).source_location
98
+ [m] + clazz.method(m).source_location
99
+ elsif instance.respond_to?(m) && instance.method(m).source_location
100
+ [m] + instance.method(m).source_location
101
+ end
102
+ end.compact
103
+ location_map = {}
104
+ source_locations.uniq { |_, path,| path }.each do |method, path, line|
105
+ next unless File.exist?(path)
106
+
107
+ source = File.open(path).read
108
+ visitor = ClassVisitor.new(clazz.ancestors)
109
+ Prism.parse(source).value.accept(visitor)
110
+ visitor.found.each do |name, def_location|
111
+ (location_map[name] ||= { name: name, locations: [], methods: [] })[:locations] << [path, def_location]
112
+ end
113
+ 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]]}}
130
+ location_map
131
+ end
132
+ end
133
+ end
134
+
135
+ module M1
136
+ def m1 = :m1
137
+ end
138
+
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
165
+ end
166
+ end
167
+
168
+ class MyClass < NS::C2
169
+ include M1
170
+ include M2
171
+ def c = :c
172
+ def self.singleton_c = :singleton_c
173
+ end
@@ -0,0 +1,4 @@
1
+ module Objenealogist
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: objenealogist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koji NAKAMURA
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: object + genealogist -> objenealogist
13
+ email:
14
+ - kozy4324@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - Rakefile
22
+ - lib/objenealogist.rb
23
+ - lib/objenealogist/version.rb
24
+ - sig/objenealogist.rbs
25
+ homepage: https://github.com/kozy4324/objenealogist
26
+ licenses:
27
+ - MIT
28
+ metadata:
29
+ allowed_push_host: https://rubygems.org
30
+ homepage_uri: https://github.com/kozy4324/objenealogist
31
+ source_code_uri: https://github.com/kozy4324/objenealogist
32
+ changelog_uri: https://github.com/kozy4324/objenealogist/releases
33
+ rubygems_mfa_required: 'true'
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.2.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 4.0.3
49
+ specification_version: 4
50
+ summary: object + genealogist -> objenealogist
51
+ test_files: []