From aa82bdf92953824fd35db4a734bf6effa8de3329 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Thu, 11 Feb 2010 23:41:16 +0100 Subject: moves Class#reachable? to Module#reachable?, bases implementation on anonymous? and constantize, and adds test coverage --- activesupport/lib/active_support/core_ext/class/subclasses.rb | 9 +++------ activesupport/lib/active_support/core_ext/module.rb | 1 + activesupport/lib/active_support/core_ext/module/reachable.rb | 10 ++++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/module/reachable.rb (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb index c166ce8079..bbd8f5aef6 100644 --- a/activesupport/lib/active_support/core_ext/class/subclasses.rb +++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb @@ -1,4 +1,5 @@ -require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/reachable' class Class #:nodoc: # Returns an array with the names of the subclasses of +self+ as strings. @@ -8,10 +9,6 @@ class Class #:nodoc: Class.subclasses_of(self).map { |o| o.to_s } end - def reachable? #:nodoc: - eval("defined?(::#{self}) && ::#{self}.equal?(self)") - end - # Rubinius if defined?(Class.__subclasses__) def descendents @@ -51,7 +48,7 @@ class Class #:nodoc: def self.subclasses_of(*superclasses) #:nodoc: subclasses = [] superclasses.each do |klass| - subclasses.concat klass.descendents.select {|k| k.name.blank? || k.reachable?} + subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?} end subclasses end diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index c907445d90..07bac29a9f 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -1,6 +1,7 @@ require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/module/reachable' require 'active_support/core_ext/module/attribute_accessors' require 'active_support/core_ext/module/attr_internal' require 'active_support/core_ext/module/attr_accessor_with_default' diff --git a/activesupport/lib/active_support/core_ext/module/reachable.rb b/activesupport/lib/active_support/core_ext/module/reachable.rb new file mode 100644 index 0000000000..443d2c3d53 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/reachable.rb @@ -0,0 +1,10 @@ +require 'active_support/core_ext/module/anonymous' +require 'active_support/core_ext/string/inflections' + +class Module + def reachable? #:nodoc: + !anonymous? && name.constantize.equal?(self) + rescue NameError + false + end +end -- cgit v1.2.3