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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 951cc96c6057b240c86ebcaba995b6635945e80a6a124bdbf6b0aadeb44832fa
4
- data.tar.gz: c4401fde554422a401cbd2f28c64748f2453c06a120585914fa6fc895add4dad
3
+ metadata.gz: 3bf00f425eca6902d2ab65a19c0b125f6a3a7462534f7aca0451097d6598f71e
4
+ data.tar.gz: 6c70cd4562686b12755f94bc0c4d40f702a6ee787eecc3b73c051a8b4a653bfd
5
5
  SHA512:
6
- metadata.gz: 596794eddd578b8f476b4eb18a173d2da6b12a6db44cde8feb03fc55468811ac99b0c96b6503ebc0b91f7d57e7792ed5223f43afefead7a9370970f8b708d53b
7
- data.tar.gz: ba3ee209ea35ad5644f0ec2ba0591b8ecd54a04cea0daab6383e5b1ab56218249bf074500f226c417fbe150f0708403213f3573cb248a68eac9fdf7cb0345901
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.1"
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,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 '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
 
@@ -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 != ::BasicObject
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 && locations[:locations]&.any? && show_locations
96
- " (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(", ")})"
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
- source_locations = methods.map do |m|
107
+ location_map = {}
108
+ methods.map do |m|
106
109
  if clazz.respond_to?(m) && clazz.method(m).source_location
107
- [m] + clazz.method(m).source_location
110
+ clazz.method(m).source_location[0]
108
111
  elsif instance.respond_to?(m) && instance.method(m).source_location
109
- [m] + instance.method(m).source_location
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.open(path).read
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] ||= { name: name, locations: [], methods: [] })[:locations] << [path, def_location]
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
- module M5
149
- def m5 = :m5
150
- end
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
- module NS
158
- class C2 < C1
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 MyClass < NS::C2
165
- include M1
166
- include M2
167
- def c = :c
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
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.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji NAKAMURA