Skip to content

Commit

Permalink
Move descendant_get from Object to Class
Browse files Browse the repository at this point in the history
This is technically a breaking change, but it really wasn't possible to
call this on Objects previously.  Most callers called this on Class.

Fixes #62
  • Loading branch information
Fryguy committed Jan 31, 2020
1 parent 37ce32e commit 1a023c5
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 50 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS
#### Class

* core_ext/class/hierarchy.rb
* `#descendant_get` - Returns the descendant with a given name
* `#hierarchy` - Returns a tree-like Hash structure of all descendants.
* `#lineage` - Returns an Array of all superclasses.
* `#leaf_subclasses` - Returns an Array of all descendants which have no subclasses.
Expand Down Expand Up @@ -82,8 +83,6 @@ MoreCoreExtensions are a set of core extensions beyond those provided by ActiveS

#### Object

* core_ext/module/descendants.rb
* `#descendant_get` - Returns the descendant with a given name
* core_ext/module/namespace.rb
* `#in_namespace?` - Returns whether or not the object is in the given namespace

Expand Down
10 changes: 10 additions & 0 deletions lib/more_core_extensions/core_ext/class/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@

module MoreCoreExtensions
module ClassHierarchy
#
# Retrieve a descendant by its name
#
def descendant_get(desc_name)
return self if desc_name == name || desc_name.nil?
klass = descendants.find { |desc| desc.name == desc_name }
raise ArgumentError, "#{desc_name} is not a descendant of #{name}" unless klass
klass
end

# Returns a tree-like Hash structure of all descendants.
#
# require 'socket'
Expand Down
1 change: 0 additions & 1 deletion lib/more_core_extensions/core_ext/object.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
require 'active_support/core_ext/object/blank'
require 'more_core_extensions/core_ext/object/namespace'
require 'more_core_extensions/core_ext/object/descendants'
17 changes: 0 additions & 17 deletions lib/more_core_extensions/core_ext/object/descendants.rb

This file was deleted.

18 changes: 18 additions & 0 deletions spec/core_ext/class/hierarchy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require 'socket' # for a more interesting hierarchy for specs

describe Class do
context "#descendant_get" do
it "returns self if specified" do
expect(IO.descendant_get("IO")).to eq(IO)
end

it "returns a direct descendant" do
expect(IO.descendant_get("BasicSocket")).to eq(BasicSocket)
end

it "returns a transitive descendant" do
expect(IO.descendant_get("IPSocket")).to eq(IPSocket)
end

it "raises an error if no descendant is found" do
expect { IO.descendant_get("Foo") }.to raise_error(ArgumentError, "Foo is not a descendant of IO")
end
end

it "#hierarchy" do
expect(IO.hierarchy).to eq(
BasicSocket => {
Expand Down
30 changes: 0 additions & 30 deletions spec/core_ext/object/descendants_spec.rb

This file was deleted.

0 comments on commit 1a023c5

Please sign in to comment.