diff --git a/README.md b/README.md index 3bd37c8..9f57620 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/lib/more_core_extensions/core_ext/class/hierarchy.rb b/lib/more_core_extensions/core_ext/class/hierarchy.rb index 7e2311c..df62886 100644 --- a/lib/more_core_extensions/core_ext/class/hierarchy.rb +++ b/lib/more_core_extensions/core_ext/class/hierarchy.rb @@ -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' diff --git a/lib/more_core_extensions/core_ext/object.rb b/lib/more_core_extensions/core_ext/object.rb index 2b1b7b0..377468c 100644 --- a/lib/more_core_extensions/core_ext/object.rb +++ b/lib/more_core_extensions/core_ext/object.rb @@ -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' diff --git a/lib/more_core_extensions/core_ext/object/descendants.rb b/lib/more_core_extensions/core_ext/object/descendants.rb deleted file mode 100644 index bea237b..0000000 --- a/lib/more_core_extensions/core_ext/object/descendants.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'active_support/core_ext/class/subclasses' - -module MoreCoreExtensions - module Descendants - # - # 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 - end -end - -Object.send(:include, MoreCoreExtensions::Descendants) diff --git a/spec/core_ext/class/hierarchy_spec.rb b/spec/core_ext/class/hierarchy_spec.rb index 7357e56..baf923e 100644 --- a/spec/core_ext/class/hierarchy_spec.rb +++ b/spec/core_ext/class/hierarchy_spec.rb @@ -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 => { diff --git a/spec/core_ext/object/descendants_spec.rb b/spec/core_ext/object/descendants_spec.rb deleted file mode 100644 index 3a7ef7c..0000000 --- a/spec/core_ext/object/descendants_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -describe Class do - context ".descendant_get" do - let!(:base_class) do - Class.new do - def self.name - "Base" - end - end - end - let!(:subclass_class) do - Class.new(base_class) do - def self.name - "SubClass" - end - end - end - - it "returns self if specified" do - expect(subclass_class.descendant_get("SubClass")).to eq(subclass_class) - end - - it "returns the correct descendant" do - expect(base_class.descendant_get("SubClass")).to eq(subclass_class) - end - - it "raises an error if no descendant is found" do - expect { base_class.descendant_get('Foo') }.to raise_error(ArgumentError, "Foo is not a descendant of Base") - end - end -end