Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rails Lighthouse ticket #410

View 0001-Add-failing-lexical-lookup-test-case.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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
View 0001-Add-failing-lexical-lookup-test-case.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
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
View 0001-Add-failing-lexical-lookup-test-case.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
View 0001-Add-failing-lexical-lookup-test-case.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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
View 0001-Add-failing-lexical-lookup-test-case.patch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
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
Something went wrong with that request. Please try again.