-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from Fryguy/class_hierarchy
Add Class#hierarchy and Class#lineage
- Loading branch information
Showing
5 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'more_core_extensions/core_ext/class/hierarchy' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'active_support/core_ext/class/subclasses' | ||
require 'active_support/core_ext/object/try' | ||
|
||
module MoreCoreExtensions | ||
module ClassHierarchy | ||
# Returns a tree-like Hash structure of all descendants. | ||
# | ||
# require 'socket' | ||
# IO.hierarchy | ||
# # => {BasicSocket=> | ||
# # {Socket=>{}, | ||
# # IPSocket=>{TCPSocket=>{TCPServer=>{}}, UDPSocket=>{}}, | ||
# # UNIXSocket=>{UNIXServer=>{}}}, | ||
# # File=>{}} | ||
def hierarchy | ||
subclasses.each_with_object({}) { |k, h| h[k] = k.hierarchy } | ||
end | ||
|
||
# Returns an Array of all superclasses. | ||
# | ||
# require 'socket' | ||
# TCPServer.lineage | ||
# # => [TCPSocket, IPSocket, BasicSocket, IO, Object, BasicObject] | ||
def lineage | ||
superclass.nil? ? [] : superclass.lineage.unshift(superclass) | ||
end | ||
end | ||
end | ||
|
||
Class.send(:include, MoreCoreExtensions::ClassHierarchy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'socket' # for a more interesting hierarchy for specs | ||
|
||
describe Class do | ||
it "#hierarchy" do | ||
expect(IO.hierarchy).to eq( | ||
BasicSocket => { | ||
Socket => {}, | ||
IPSocket => {TCPSocket => {TCPServer => {}}, UDPSocket => {}}, | ||
UNIXSocket => {UNIXServer => {}} | ||
}, | ||
File => {} | ||
) | ||
end | ||
|
||
it "#lineage" do | ||
expect(TCPServer.lineage).to eq [TCPSocket, IPSocket, BasicSocket, IO, Object, BasicObject] | ||
end | ||
end |