summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2010-04-08 02:40:45 +0200
committerFlorian Frank <flori@ping.de>2010-04-08 02:40:45 +0200
commitb2a5be0eef9095154cc8c7751d0318a565dad6da (patch)
tree386b576e336bcd9e6c82b59aaec45c70f56fcb55
parentc3e8dd92f04b010366e4d7152c83c7486af93e1e (diff)
parenta294a83f4d22901651d09c06063eb20d3b2290b8 (diff)
downloadjson-b2a5be0eef9095154cc8c7751d0318a565dad6da.tar.gz
Merged in const_missing triggering from v1.2
Merge commit 'a294a83f4d22901651d09c06063eb20d3b2290b8' Conflicts: VERSION lib/json/version.rb
-rw-r--r--CHANGES2
-rw-r--r--ext/json/ext/parser.c5
-rw-r--r--ext/json/ext/parser.rl5
-rw-r--r--lib/json/common.rb10
4 files changed, 15 insertions, 7 deletions
diff --git a/CHANGES b/CHANGES
index 1db68cc..7f6a557 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,5 @@
+2010-04-07 (1.2.4)
+ * Triger const_missing callback to make Rails' dynamic class loading work.
2010-03-11 (1.2.3)
* Added a State#[] method which returns an attribute's value in order to
increase duck type compatibility to Hash.
diff --git a/ext/json/ext/parser.c b/ext/json/ext/parser.c
index 2272fa2..0dadf74 100644
--- a/ext/json/ext/parser.c
+++ b/ext/json/ext/parser.c
@@ -79,7 +79,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
- i_array_class, i_key_p;
+ i_array_class, i_key_p, i_deep_const_get;
#line 108 "parser.rl"
@@ -440,7 +440,7 @@ case 26:
if (RTEST(json->create_id)) {
VALUE klassname = rb_hash_aref(*result, json->create_id);
if (!NIL_P(klassname)) {
- VALUE klass = rb_path2class(StringValueCStr(klassname));
+ VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) {
*result = rb_funcall(klass, i_json_create, 1, *result);
}
@@ -1914,6 +1914,7 @@ void Init_parser()
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
i_key_p = rb_intern("key?");
+ i_deep_const_get = rb_intern("deep_const_get");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
diff --git a/ext/json/ext/parser.rl b/ext/json/ext/parser.rl
index 012ca31..6e196cf 100644
--- a/ext/json/ext/parser.rl
+++ b/ext/json/ext/parser.rl
@@ -77,7 +77,7 @@ static VALUE CNaN, CInfinity, CMinusInfinity;
static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
i_chr, i_max_nesting, i_allow_nan, i_symbolize_names, i_object_class,
- i_array_class, i_key_p;
+ i_array_class, i_key_p, i_deep_const_get;
%%{
machine JSON_common;
@@ -161,7 +161,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (RTEST(json->create_id)) {
VALUE klassname = rb_hash_aref(*result, json->create_id);
if (!NIL_P(klassname)) {
- VALUE klass = rb_path2class(StringValueCStr(klassname));
+ VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) {
*result = rb_funcall(klass, i_json_create, 1, *result);
}
@@ -771,6 +771,7 @@ void Init_parser()
i_object_class = rb_intern("object_class");
i_array_class = rb_intern("array_class");
i_key_p = rb_intern("key?");
+ i_deep_const_get = rb_intern("deep_const_get");
#ifdef HAVE_RUBY_ENCODING_H
CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
CEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
diff --git a/lib/json/common.rb b/lib/json/common.rb
index cd9d1c6..a8aa016 100644
--- a/lib/json/common.rb
+++ b/lib/json/common.rb
@@ -33,12 +33,16 @@ module JSON
# level (absolute namespace path?). If there doesn't exist a constant at
# the given path, an ArgumentError is raised.
def deep_const_get(path) # :nodoc:
- path = path.to_s
- path.split(/::/).inject(Object) do |p, c|
+ path.to_s.split(/::/).inject(Object) do |p, c|
case
when c.empty? then p
when p.const_defined?(c) then p.const_get(c)
- else raise ArgumentError, "can't find const #{path}"
+ else
+ if (c = p.const_missing(c) rescue nil)
+ c
+ else
+ raise ArgumentError, "can't find const #{path}"
+ end
end
end
end