Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/969906 to your computer and use it in GitHub Desktop.
Save AquaGeek/969906 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #410
From 99464feddc512c061303ea33ef5b6b5c1a9ae7ed Mon Sep 17 00:00:00 2001
From: Ripta Pasay <github@r8y.org>
Date: Wed, 3 Dec 2008 11:37:45 -0500
Subject: [PATCH] Add failing lexical lookup test case
---
activesupport/test/inflector_test.rb | 15 +++++++++++++++
1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index d8c93dc..84f3a4e 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -2,8 +2,21 @@ require 'abstract_unit'
require 'inflector_test_cases'
module Ace
+ module Extension
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def mission_accomplished?
+ false
+ end
+ end
+ end
+
module Base
class Case
+ include Extension
end
end
end
@@ -168,6 +181,8 @@ class InflectorTest < Test::Unit::TestCase
def test_constantize_does_lexical_lookup
assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
+ assert_nothing_raised { Ace::Base::Case::ClassMethods }
+ assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
end
def test_ordinal
--
1.5.6.4
From 7d9e601812d0f2fa5bd355e83987bff32c1b4695 Mon Sep 17 00:00:00 2001
From: Andrew White <andyw@pixeltrix.co.uk>
Date: Thu, 19 Aug 2010 17:51:06 +0100
Subject: [PATCH] Use constant.constants.member? rather than const_defined? as the former will include constants in ancestors [#410 state:resolved]
---
.../lib/active_support/inflector/methods.rb | 6 ++++--
activesupport/test/inflector_test.rb | 15 +++++++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index de49750..bc8e5cc 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -110,7 +110,8 @@ module ActiveSupport
constant = Object
names.each do |name|
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name)}
+ constant = module_with_constant ? module_with_constant.const_get(name) : constant.const_missing(name)
end
constant
end
@@ -121,7 +122,8 @@ module ActiveSupport
constant = Object
names.each do |name|
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
+ module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name, false)}
+ constant = module_with_constant ? module_with_constant.const_get(name, false) : constant.const_missing(name)
end
constant
end
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index 2990177..c36f543 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -4,8 +4,21 @@ require 'active_support/inflector'
require 'inflector_test_cases'
module Ace
+ module Extension
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def mission_accomplished?
+ false
+ end
+ end
+ end
+
module Base
class Case
+ include Extension
end
end
end
@@ -176,6 +189,8 @@ class InflectorTest < Test::Unit::TestCase
def test_constantize_does_lexical_lookup
assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
+ assert_nothing_raised { Ace::Base::Case::ClassMethods }
+ assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
end
def test_ordinal
--
1.7.1
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index a4fd619..fcbfbaa 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -276,10 +276,12 @@ module Inflector
# NameError is raised when the name is not in CamelCase or the constant is
# unknown.
def constantize(camel_cased_word)
- unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
- raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
+ list = camel_cased_word.split("::")
+ list.shift if list.first.blank?
+ obj = Object
+ list.each do |x|
+ obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
end
-
- Object.module_eval("::#{$1}", __FILE__, __LINE__)
+ obj
end
# Turns a number into an ordinal string used to denote the position in an
require 'benchmark'
class String
def blank?; empty? || strip.empty?; end
end
def new_constantize(camel_cased_word)
list = camel_cased_word.split('::')
list.shift if list.first.blank?
obj = Object
list.each do |x|
obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
end
obj
end
def old_constantize(camel_cased_word)
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
end
Object.module_eval("::#{$1}", __FILE__, __LINE__)
end
puts "\n'String'"
Benchmark.bm do |x|
x.report('const') { new_constantize('String') }
x.report('eval') { old_constantize('String') }
end
module A; module B; module C; module D; module E; module F; end; end; end; end; end; end
puts "\n'A::B'"
Benchmark.bm do |x|
x.report('const') { new_constantize('A::B') }
x.report('eval') { old_constantize('A::B') }
end
puts "\n'A::B::C'"
Benchmark.bm do |x|
x.report('const') { new_constantize('A::B::C') }
x.report('eval') { old_constantize('A::B::C') }
end
From 17114235ce08705c58b6b6b407279e17aa5e254b Mon Sep 17 00:00:00 2001
From: Frederick Cheung <frederick.cheung@gmail.com>
Date: Sun, 14 Dec 2008 10:07:06 +0000
Subject: [PATCH] Make constantize look into ancestors
---
activesupport/lib/active_support/inflector.rb | 7 ++++---
activesupport/test/inflector_test.rb | 15 +++++++++++++++
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index 4921b99..6f04107 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -353,10 +353,10 @@ module ActiveSupport
def constantize(camel_cased_word)
names = camel_cased_word.split('::')
names.shift if names.empty? || names.first.empty?
-
constant = Object
names.each do |name|
- constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
+ module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name)}
+ constant = module_with_constant ? module_with_constant.const_get(name) : constant.const_missing(name)
end
constant
end
@@ -367,7 +367,8 @@ module ActiveSupport
constant = Object
names.each do |name|
- constant = constant.const_get(name, false) || constant.const_missing(name)
+ module_with_constant = constant.ancestors.detect {|mod| mod.const_defined?(name, false)}
+ constant = module_with_constant ? module_with_constant.const_get(name, false) : constant.const_missing(name)
end
constant
end
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index d8c93dc..84f3a4e 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -2,8 +2,21 @@ require 'abstract_unit'
require 'inflector_test_cases'
module Ace
+ module Extension
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def mission_accomplished?
+ false
+ end
+ end
+ end
+
module Base
class Case
+ include Extension
end
end
end
@@ -168,6 +181,8 @@ class InflectorTest < Test::Unit::TestCase
def test_constantize_does_lexical_lookup
assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
+ assert_nothing_raised { Ace::Base::Case::ClassMethods }
+ assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
end
def test_ordinal
--
1.6.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment