Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32afe4b

Browse files
committed
Split class and instance methods
Show them separately on Object page, like on Ruby-Doc.org
1 parent d5a47f4 commit 32afe4b

File tree

15 files changed

+152
-91
lines changed

15 files changed

+152
-91
lines changed

app/graphql/types/ruby_object_type.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class RubyObjectType < Types::BaseObject
1111
field :superclass, String, null: true
1212
field :included_modules, [String], null: false
1313
field :version, String, null: false
14-
field :ruby_methods, [Types::RubyMethodType], null: false
14+
field :ruby_class_methods, [Types::RubyMethodType], null: false
15+
field :ruby_instance_methods, [Types::RubyMethodType], null: false
1516
field :path, String, null: false
1617

1718
def path

app/helpers/search_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def escape_method_name(method_name)
99
end
1010

1111
def method_anchor(method)
12-
"method-#{method.instance_method? ? "i" : "c"}-#{escape_method_name(method.name)}"
12+
"method-#{method.method_type == "instance" ? "i" : "c"}-#{escape_method_name(method.name)}"
1313
end
1414

1515
def result_url(result, version:)

app/models/ruby_method.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def object_constant
2323
body[:object_constant]
2424
end
2525

26-
def instance_method?
27-
method_type == "instance_method"
28-
end
29-
30-
def class_method?
31-
method_type == "class_method"
26+
def type_identifier
27+
case method_type
28+
when "class" then "::"
29+
when "instance" then "#"
30+
else raise "Unknown type of method: #{method_type}"
31+
end
3232
end
3333

3434
def identifier
@@ -69,14 +69,4 @@ def to_hash
6969
metadata: metadata
7070
}
7171
end
72-
73-
private
74-
75-
def type_identifier
76-
if instance_method?
77-
"#"
78-
elsif class_method?
79-
"::"
80-
end
81-
end
8272
end

app/models/ruby_object.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ def ==(other)
5656
end
5757

5858
# This is be empty in search pages
59-
def ruby_methods
60-
@ruby_methods ||= body[:methods].collect { |m| RubyMethod.new(m) }
59+
def ruby_class_methods
60+
@ruby_class_methods ||=
61+
body[:class_methods].collect { |m| RubyMethod.new(m) }
62+
end
63+
64+
def ruby_instance_methods
65+
@ruby_instance_methods ||=
66+
body[:instance_methods].collect { |m| RubyMethod.new(m) }
6167
end
6268

6369
def superclass
@@ -81,7 +87,8 @@ def to_hash
8187
type: :object,
8288
description: description,
8389
autocomplete: autocomplete,
84-
methods: ruby_methods.collect(&:to_hash),
90+
class_methods: ruby_class_methods.collect(&:to_hash),
91+
instance_methods: ruby_instance_methods.collect(&:to_hash),
8592
constant: constant,
8693
superclass: superclass&.constant,
8794
included_modules: included_modules.map(&:constant),

app/repositories/ruby_object_repository.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class RubyObjectRepository
1010
mapping do
1111
indexes :name, type: :text
1212
indexes :description, type: :text, index: false
13-
indexes :methods, type: :nested
13+
indexes :class_methods, type: :nested
14+
indexes :instance_methods, type: :nested
1415
indexes :superclass, type: :text
1516
indexes :included_modules, type: :text
1617
indexes :metadata, type: :nested

app/services/search/filters/is.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def self.filter_for(value)
1414
when "method"
1515
[{term: {type: {value: :method}}}]
1616
when "class-method", "class-method", "cmethod"
17-
[{term: {method_type: {value: :class_method}}}]
17+
[{term: {method_type: {value: :class}}}]
1818
when "instance-method", "instance-method", "imethod", "#"
19-
[{term: {method_type: {value: :instance_method}}}]
19+
[{term: {method_type: {value: :instance}}}]
2020
else
2121
[]
2222
end

app/views/objects/_sidebar.html.slim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ div class="hidden lg:block w-1/4"
1515
= render "objects/sidebar/section/link",
1616
title: included_module.constant,
1717
href: object_path(object: included_module.path)
18-
- unless @object.ruby_methods.select(&:class_method?).empty?
18+
- unless @object.ruby_class_methods.empty?
1919
= render "objects/sidebar/section", title: 'Class Methods' do
2020
ul class="font-mono text-sm"
21-
- @object.ruby_methods.select(&:class_method?).sort_by(&:name).each do |m|
21+
- @object.ruby_class_methods.sort_by(&:name).each do |m|
2222
li
2323
= render "objects/sidebar/section/link",
2424
title: ":: #{m.name}", href: "##{method_anchor(m)}"
25-
- unless @object.ruby_methods.select(&:instance_method?).empty?
25+
- unless @object.ruby_instance_methods.empty?
2626
= render "objects/sidebar/section", title: 'Instance Methods' do
2727
ul class="font-mono text-sm"
28-
- @object.ruby_methods.select(&:instance_method?).sort_by(&:name).each do |m|
28+
- @object.ruby_instance_methods.sort_by(&:name).each do |m|
2929
li
3030
= render "objects/sidebar/section/link",
3131
title: "# #{m.name}", href: "##{method_anchor(m)}"

app/views/objects/show.html.slim

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,7 @@ div class="max-w-screen-xl mx-auto px-3 md:px-0 lg:flex"
2020
| Module
2121
div class="ruby-documentation"
2222
== @object.description
23-
hr
24-
- @object.ruby_methods.sort_by(&:name).each do |m|
25-
div class="md:p-3 py-3 my-3"
26-
a style="display: block; position: relative; top: -80px; visibility: hidden;" id="#{method_anchor m}"
27-
div class="flex flex-wrap"
28-
div class="w-full md:w-10/12"
29-
- if m.call_sequence.empty?
30-
h4 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold"
31-
a href="##{method_anchor(m)}"
32-
| #{m.name}
33-
- else
34-
a href="##{method_anchor(m)}"
35-
- m.call_sequence.each do |seq|
36-
h4 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold"
37-
= seq
38-
div class="flex md:justify-end w-full md:w-2/12 mt-3 md:mt-0 font-mono"
39-
- if m.instance_method?
40-
span class="px-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 algin-middle cursor-default" title="Instance Method"
41-
| #
42-
- elsif m.class_method?
43-
span class="px-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 algin-middle cursor-default" title="Class Method"
44-
| ::
45-
a class="px-1 ml-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 align-middle hover:bg-gray-300 hover:text-gray-800 dark:hover:bg-gray-900 dark:hover:text-gray-400 hover:fill-current" href="#{github_url m}" target="_blank" rel="noopener" title="View source on Github"
46-
i class="fab fa-github"
47-
div class="ruby-documentation py-1"
48-
- if m.description.empty?
49-
div
50-
| No documentation available
51-
- else
52-
== m.description
23+
= render "objects/show/methods",
24+
methods: @object.ruby_class_methods, title: "Class Methods"
25+
= render "objects/show/methods",
26+
methods: @object.ruby_instance_methods, title: "Instance Methods"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
div class="md:px-3 my-3"
2+
h3 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold"
3+
= title
4+
hr
5+
- methods.sort_by(&:name).each do |m|
6+
div class="md:p-3 py-3 my-3"
7+
a style="display: block; position: relative; top: -80px; visibility: hidden;" id="#{method_anchor m}"
8+
div class="flex flex-wrap"
9+
div class="w-full md:w-10/12"
10+
- if m.call_sequence.empty?
11+
h4 class="lg:text-xl text-md text-gray-900 dark:text-gray-200 font-semibold"
12+
a href="##{method_anchor(m)}"
13+
= m.name
14+
- else
15+
a href="##{method_anchor(m)}"
16+
- m.call_sequence.each do |seq|
17+
h4 class="lg:text-xl text-md text-gray-900 dark:text-gray-200 font-semibold"
18+
= seq
19+
div class="flex md:justify-end w-full md:w-2/12 mt-3 md:mt-0 font-mono"
20+
span class="px-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 algin-middle cursor-default" title="#{m.method_type.capitalize} Method"
21+
= m.type_identifier
22+
a class="px-1 ml-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 align-middle hover:bg-gray-300 hover:text-gray-800 dark:hover:bg-gray-900 dark:hover:text-gray-400 hover:fill-current" href="#{github_url m}" target="_blank" rel="noopener" title="View source on Github"
23+
i class="fab fa-github"
24+
div class="ruby-documentation py-1"
25+
- if m.description.empty?
26+
div
27+
| No documentation available
28+
- else
29+
== m.description

lib/rubyapi_rdoc_generator.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,24 @@ def generate_objects
3636

3737
@documentation.each do |doc|
3838
next if skip_namespace? doc.full_name
39-
methods = []
39+
40+
methods = {"class" => [], "instance" => []}
4041

4142
doc.method_list.each do |method_doc|
42-
next if methods.find { |m| m[:name] == method_doc.name }
43+
method_name = method_doc.name
44+
45+
if methods.any? do |_type, set|
46+
set.find { |m| m[:name] == method_name }
47+
end
48+
next
49+
end
4350

44-
methods << {
45-
name: method_doc.name,
51+
method_type = method_doc.type
52+
methods[method_type] << {
53+
name: method_name,
4654
description: clean_description(method_doc.description),
4755
object_constant: doc.full_name,
48-
method_type: "#{method_doc.type}_method",
56+
method_type: method_type,
4957
source_location: "#{@release.version}:#{method_path(method_doc)}:#{method_doc.line}",
5058
call_sequence: method_doc.call_seq ? method_doc.call_seq.strip.split("\n").map { |s| s.gsub "->", "→" } : "",
5159
metadata: {
@@ -66,7 +74,8 @@ def generate_objects
6674
objects << RubyObject.new(
6775
name: doc.name,
6876
description: clean_description(doc.description),
69-
methods: methods,
77+
class_methods: methods["class"],
78+
instance_methods: methods["instance"],
7079
constant: doc.full_name,
7180
object_type: "#{doc.type}_object",
7281
superclass: superclass,
@@ -86,7 +95,8 @@ def index_object(object)
8695
object_repository.save(object)
8796
search_repository.save(object)
8897

89-
object.ruby_methods.each { |m| search_repository.save m }
98+
object.ruby_class_methods.each { |m| search_repository.save m }
99+
object.ruby_instance_methods.each { |m| search_repository.save m }
90100
end
91101

92102
def object_repository

0 commit comments

Comments
 (0)