Skip to content

Split class and instance methods in model #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/javascript/packs/app/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pre {
.ruby-operator { color: #5FB3B3; }

p {
@apply py-3;
@apply my-3;
}

.overflow-wrap-break-word {
Expand Down
21 changes: 9 additions & 12 deletions app/models/ruby_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ def object_constant
body[:object_constant]
end

def class_method?
method_type == "class_method"
end

def instance_method?
method_type == "instance_method"
end

def class_method?
method_type == "class_method"
def type_identifier
if class_method? then "::"
elsif instance_method? then "#"
else raise "Unknown type of method: #{method_type}"
end
end

def identifier
Expand Down Expand Up @@ -86,14 +93,4 @@ def to_hash
metadata: metadata
}
end

private

def type_identifier
if instance_method?
"#"
elsif class_method?
"::"
end
end
end
10 changes: 10 additions & 0 deletions app/models/ruby_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def ruby_methods
@ruby_methods ||= body[:methods].collect { |m| RubyMethod.new(m) }
end

def ruby_class_methods
@ruby_class_methods ||=
ruby_methods.select(&:class_method?).sort_by(&:name)
end

def ruby_instance_methods
@ruby_instance_methods ||=
ruby_methods.select(&:instance_method?).sort_by(&:name)
end

def superclass
return @superclass if defined?(@superclass)

Expand Down
8 changes: 4 additions & 4 deletions app/views/objects/_sidebar.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ div class="hidden lg:block w-1/4"
= render "objects/sidebar/section/link",
title: included_module.constant,
href: object_path(object: included_module.path)
- unless @object.ruby_methods.select(&:class_method?).empty?
- unless @object.ruby_class_methods.empty?
= render "objects/sidebar/section", title: 'Class Methods' do
ul class="font-mono text-sm"
- @object.ruby_methods.select(&:class_method?).sort_by(&:name).each do |m|
- @object.ruby_class_methods.each do |m|
li
= render "objects/sidebar/section/link",
title: ":: #{m.name}", href: "##{method_anchor(m)}"
- unless @object.ruby_methods.select(&:instance_method?).empty?
- unless @object.ruby_instance_methods.empty?
= render "objects/sidebar/section", title: 'Instance Methods' do
ul class="font-mono text-sm"
- @object.ruby_methods.select(&:instance_method?).sort_by(&:name).each do |m|
- @object.ruby_instance_methods.each do |m|
li
= render "objects/sidebar/section/link",
title: "# #{m.name}", href: "##{method_anchor(m)}"
36 changes: 7 additions & 29 deletions app/views/objects/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
div class="max-w-screen-xl mx-auto px-3 md:px-0 lg:flex"
= render "objects/sidebar"
div class="w-full mt-16 lg:mt-20 lg:w-3/4"
div class="md:p-3 py-3"
div class="m-3"
div class="flex flex-wrap"
div class="w-full md:w-6/12"
h1 class="lg:text-3xl text-xl text-gray-800 dark:text-gray-200 font-semibold"
Expand All @@ -20,31 +20,9 @@ div class="max-w-screen-xl mx-auto px-3 md:px-0 lg:flex"
| Module
div class="ruby-documentation"
== @object.description
hr
- @object.ruby_methods.sort_by(&:name).each do |m|
div class="md:p-3 py-3 my-3"
a style="display: block; position: relative; top: -80px; visibility: hidden;" id="#{method_anchor m}"
div class="flex flex-wrap"
div class="w-full md:w-10/12"
a href="##{method_anchor(m)}"
- m.call_sequence.each do |seq|
h4 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold"= seq

div class="flex md:justify-end w-full md:w-2/12 mt-3 md:mt-0 font-mono"
- if m.instance_method?
span class="px-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 algin-middle cursor-default" title="Instance Method"
| #
- elsif m.class_method?
span class="px-2 h-6 inline-block rounded bg-gray-200 dark:bg-gray-700 algin-middle cursor-default" title="Class Method"
| ::
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"
i class="fab fa-github"
div class="ruby-documentation py-1"
- if m.alias?
div
| An alias for <a href="#{m.alias_path}" class="font-bold">#{m.alias_name}</a>
- elsif m.description.empty?
div
| No documentation available
- else
== m.description
- unless @object.ruby_class_methods.empty?
= render "objects/show/methods",
methods: @object.ruby_class_methods, title: "Class Methods"
- unless @object.ruby_instance_methods.empty?
= render "objects/show/methods",
methods: @object.ruby_instance_methods, title: "Instance Methods"
27 changes: 27 additions & 0 deletions app/views/objects/show/_methods.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
div class="md:px-3 my-3"
h3 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold mt-8"
= title
hr
- methods.each do |m|
div class="md:px-3 my-3"
a style="display: block; position: relative; top: -80px; visibility: hidden;" id="#{method_anchor m}"
div class="flex flex-wrap"
div class="w-full md:w-10/12"
a href="##{method_anchor(m)}"
- m.call_sequence.each do |seq|
h4 class="lg:text-2xl text-lg text-gray-900 dark:text-gray-200 font-semibold"= seq

div class="flex md:justify-end w-full md:w-2/12 mt-3 md:mt-0 font-mono"
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"
= m.type_identifier
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"
i class="fab fa-github"
div class="ruby-documentation py-1"
- if m.alias?
div
| An alias for <a href="#{m.alias_path}" class="font-bold">#{m.alias_name}</a>
- elsif m.description.empty?
div
| No documentation available
- else
== m.description
12 changes: 11 additions & 1 deletion test/controllers/objects_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def setup
string_info[:methods] << {
name: "foo",
description: "<h1>Hello World</h1>",
method_type: "instance_method",
method_type: "class_method",
object_constant: "String",
superclass: "Object",
included_modules: [],
Expand All @@ -88,6 +88,16 @@ def setup
"foo(arg1, arg2)"
]
}
string_info[:methods] << {
name: "bar",
description: "<h1>Hello World</h1>",
method_type: "instance_method",
object_constant: "String",
superclass: "Object",
included_modules: [],
source_location: "2.6.4:string.c:L3",
call_sequence: []
}

string = RubyObject.new(string_info)

Expand Down
4 changes: 2 additions & 2 deletions test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ApplicationHelperTest < ActionView::TestCase
test "github_url" do
method = ruby_object(String).ruby_methods.first
assert_equal github_url(method), "https://github.com/ruby/ruby/blob/v2_6_4/string.c#L76"
method = ruby_object(String).ruby_class_methods.first
assert_equal github_url(method), "https://github.com/ruby/ruby/blob/v2_6_4/string.c#L3"
end
end
6 changes: 4 additions & 2 deletions test/models/ruby_method_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setup
}

@method = RubyMethod.new(attributes)
@class_method = RubyMethod.new(method_type: "class_method")
end

test "required attribues" do
Expand All @@ -38,11 +39,12 @@ def setup

test "#instance_method?" do
assert_equal @method.instance_method?, true
assert_equal @class_method.instance_method?, false
end

test "#class_method?" do
method = RubyMethod.new(method_type: "class_method")
assert_equal method.class_method?, true
assert_equal @class_method.class_method?, true
assert_equal @method.class_method?, false
end

test "#identifier" do
Expand Down
87 changes: 67 additions & 20 deletions test/models/ruby_object_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ def setup
depth: 1
},
methods: [
{
name: "new",
description: "<h1>Hello World</h1>",
method_type: "class_method",
object_constant: "String",
source_location: "2.6.4:string.c:L28",
metadata: {
depth: 1
},
call_sequence: <<~G
String.new # => ""
G
},
{
name: "to_i",
description: "<h1>Hello World</h1>",
Expand Down Expand Up @@ -68,8 +81,23 @@ def setup
end

test "ruby_methods" do
method = @object.ruby_methods.first
assert_equal method.class, RubyMethod
assert_equal @object.ruby_methods.all? { |m| m.is_a?(RubyMethod) }, true
end

test "ruby_class_methods" do
assert_equal(
@object.ruby_class_methods.all? { |m| m.is_a?(RubyMethod) },
true
)
assert_equal @object.ruby_class_methods.all?(&:class_method?), true
end

test "ruby_instance_methods" do
assert_equal(
@object.ruby_instance_methods.all? { |m| m.is_a?(RubyMethod) },
true
)
assert_equal @object.ruby_instance_methods.all?(&:instance_method?), true
end

test "#to_hash" do
Expand All @@ -83,26 +111,45 @@ def setup
metadata: {
depth: 1
},
methods: [{
name: "to_i",
description: "<h1>Hello World</h1>",
type: :method,
autocomplete: "String#to_i",
object_constant: "String",
identifier: "String#to_i",
method_type: "instance_method",
source_location: "2.6.4:string.c:L54",
call_sequence: <<~G,
str.to_i # => 1
G
alias: {
path: "String.html#to_integer",
name: "to_integer"
methods: [
{
name: "new",
description: "<h1>Hello World</h1>",
type: :method,
autocomplete: "String::new",
object_constant: "String",
identifier: "String::new",
method_type: "class_method",
source_location: "2.6.4:string.c:L28",
metadata: {
depth: 1
},
call_sequence: <<~G,
String.new # => ""
G
alias: {}
},
metadata: {
depth: 1
{
name: "to_i",
description: "<h1>Hello World</h1>",
type: :method,
autocomplete: "String#to_i",
object_constant: "String",
identifier: "String#to_i",
method_type: "instance_method",
source_location: "2.6.4:string.c:L54",
metadata: {
depth: 1
},
call_sequence: <<~G,
str.to_i # => 1
G
alias: {
path: "String.html#to_integer",
name: "to_integer"
}
}
}],
],
name: "String",
object_type: "class_object",
superclass: "Object",
Expand Down
58 changes: 35 additions & 23 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,41 @@ def ruby_object(constant, object_type: :class)
metadata: {
depth: 1
},
methods: [{
name: "empty?",
description: "<h1>Hello World!</h1>",
method_type: "instance_method",
object_constant: constant.to_s,
source_location: "2.6.4:string.c:76",
metadata: {
depth: 1
},
call_sequence: []
}, {
name: "to_i",
description: "<h1>Hello World</h1>",
method_type: "instance_method",
object_constant: constant.to_s,
source_location: "2.6.4:string.c:54",
metadata: {
depth: 1
},
call_sequence: [
"str.to_i # => 1"
]
}]
methods: [
{
name: "new",
description: "<h1>Hello World!</h1>",
method_type: "class_method",
object_constant: constant.to_s,
source_location: "2.6.4:string.c:3",
metadata: {
depth: 1
},
call_sequence: []
}, {
name: "empty?",
description: "<h1>Hello World!</h1>",
method_type: "instance_method",
object_constant: constant.to_s,
source_location: "2.6.4:string.c:76",
metadata: {
depth: 1
},
call_sequence: []
}, {
name: "to_i",
description: "<h1>Hello World</h1>",
method_type: "instance_method",
object_constant: constant.to_s,
source_location: "2.6.4:string.c:54",
metadata: {
depth: 1
},
call_sequence: [
"str.to_i # => 1"
]
}
]
}

RubyObject.new(attributes)
Expand Down