diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2010-02-11 15:08:01 -0800 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2010-02-11 15:08:01 -0800 |
commit | 2b874f8e812f5fe817ee87c5f2b7cd5addf058bc (patch) | |
tree | 0c6359d89894dd6f42e35a0583141bc28c089417 | |
parent | eea28b07cf25d5c79f8edeb8bef5e0483b489425 (diff) | |
parent | a506bac58616db2dc2b470a2416deed398645916 (diff) | |
download | rails-2b874f8e812f5fe817ee87c5f2b7cd5addf058bc.tar.gz rails-2b874f8e812f5fe817ee87c5f2b7cd5addf058bc.tar.bz2 rails-2b874f8e812f5fe817ee87c5f2b7cd5addf058bc.zip |
Merge remote branch 'fxn/master'
6 files changed, 94 insertions, 7 deletions
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 41600929f4..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/anonymous.rb b/activesupport/lib/active_support/core_ext/module/anonymous.rb new file mode 100644 index 0000000000..df25a09ec9 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/anonymous.rb @@ -0,0 +1,24 @@ +require 'active_support/core_ext/object/blank' + +class Module + # A module may or may not have a name. + # + # module M; end + # M.name # => "M" + # + # m = Module.new + # m.name # => "" + # + # A module gets a name when it is first assigned to a constant. Either + # via the +module+ or +class+ keyword or by an explicit assignment: + # + # m = Module.new # creates an anonymous module + # M = m # => m gets a name here as a side-effect + # m.name # => "M" + # + def anonymous? + # Uses blank? because the name of an anonymous class is an empty + # string in 1.8, and nil in 1.9. + name.blank? + end +end 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 diff --git a/activesupport/test/core_ext/module/anonymous_test.rb b/activesupport/test/core_ext/module/anonymous_test.rb new file mode 100644 index 0000000000..7a78a3b012 --- /dev/null +++ b/activesupport/test/core_ext/module/anonymous_test.rb @@ -0,0 +1,14 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/anonymous' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module are anonymous" do + assert Module.new.anonymous? + assert Class.new.anonymous? + end + + test "a named class or module are not anonymous" do + assert !Kernel.anonymous? + assert !Object.anonymous? + end +end
\ No newline at end of file diff --git a/activesupport/test/core_ext/module/reachable_test.rb b/activesupport/test/core_ext/module/reachable_test.rb new file mode 100644 index 0000000000..72892b77d5 --- /dev/null +++ b/activesupport/test/core_ext/module/reachable_test.rb @@ -0,0 +1,41 @@ +require 'abstract_unit' +require 'active_support/core_ext/module/reachable' + +class AnonymousTest < ActiveSupport::TestCase + test "an anonymous class or module is not reachable" do + assert !Module.new.reachable? + assert !Class.new.reachable? + end + + test "ordinary named classes or modules are reachable" do + assert Kernel.reachable? + assert Object.reachable? + end + + test "a named class or module whose constant has gone is not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + assert !c.reachable? + assert !m.reachable? + end + + test "a named class or module whose constants store different objects are not reachable" do + c = eval "class C; end; C" + m = eval "module M; end; M" + + self.class.send(:remove_const, :C) + self.class.send(:remove_const, :M) + + eval "class C; end" + eval "module M; end" + + assert C.reachable? + assert M.reachable? + assert !c.reachable? + assert !m.reachable? + end +end
\ No newline at end of file |