diff options
author | Florian Frank <flori@ping.de> | 2010-04-08 02:11:12 +0200 |
---|---|---|
committer | Florian Frank <flori@ping.de> | 2010-04-08 02:33:07 +0200 |
commit | a294a83f4d22901651d09c06063eb20d3b2290b8 (patch) | |
tree | c9aeca3882c63a5891c7e5b3b89814189a2a752a | |
parent | 3db50701a4a71e49709c63483d2ba4b5a408373b (diff) | |
download | json-a294a83f4d22901651d09c06063eb20d3b2290b8.tar.gz |
Trigger const_missing mechanism for Rails
In order to allow Rails' dynamic class loading to work, the
const_missing callback must be called.
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | VERSION | 2 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.c | 5 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.rl | 5 | ||||
-rw-r--r-- | lib/json/common.rb | 10 | ||||
-rw-r--r-- | lib/json/version.rb | 2 |
6 files changed, 17 insertions, 9 deletions
@@ -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. @@ -1 +1 @@ -1.2.3 +1.2.4 diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 96530fb..ceb7919 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -26,7 +26,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; #define MinusInfinity "-Infinity" @@ -415,7 +415,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); } @@ -1875,6 +1875,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 mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); mEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be")); diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index f247b07..c4887dd 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -24,7 +24,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; #define MinusInfinity "-Infinity" @@ -136,7 +136,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); } @@ -732,6 +732,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 mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8")); mEncoding_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 2f1584e..3ce25c1 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 diff --git a/lib/json/version.rb b/lib/json/version.rb index 571612e..a306929 100644 --- a/lib/json/version.rb +++ b/lib/json/version.rb @@ -1,6 +1,6 @@ module JSON # JSON version - VERSION = '1.2.3' + VERSION = '1.2.4' VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc: VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc: VERSION_MINOR = VERSION_ARRAY[1] # :nodoc: |