Skip to content

Commit 3db5070

Browse files
committed
added [] method to State objects
1 parent 98a6817 commit 3db5070

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

CHANGES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2010-03-11 (1.2.3)
2+
* Added a State#[] method which returns an attribute's value in order to
3+
increase duck type compatibility to Hash.
14
2010-02-27 (1.2.2)
25
* Made some changes to make the building of the parser/generator compatible
36
to Rubinius.

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.2
1+
1.2.3

ext/json/ext/generator/generator.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ static VALUE mJSON, mExt, mGenerator, cState, mGeneratorMethods, mObject,
4141

4242
static ID i_to_s, i_to_json, i_new, i_indent, i_space, i_space_before,
4343
i_object_nl, i_array_nl, i_check_circular, i_max_nesting,
44-
i_allow_nan, i_pack, i_unpack, i_create_id, i_extend, i_key_p;
44+
i_allow_nan, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
45+
i_aref, i_send, i_respond_to_p;
4546

4647
typedef struct JSON_Generator_StateStruct {
4748
VALUE indent;
@@ -584,6 +585,20 @@ static VALUE cState_to_h(VALUE self)
584585
return result;
585586
}
586587

588+
/*
589+
* call-seq: [](name)
590+
*
591+
* Return the value returned by method +name+.
592+
*/
593+
static VALUE cState_aref(VALUE self, VALUE name)
594+
{
595+
GET_STATE(self);
596+
if (RTEST(rb_funcall(self, i_respond_to_p, 1, name))) {
597+
return rb_funcall(self, i_send, 1, name);
598+
} else {
599+
return Qnil;
600+
}
601+
}
587602

588603
/*
589604
* call-seq: new(opts = {})
@@ -884,6 +899,7 @@ void Init_generator()
884899
rb_define_method(cState, "forget", cState_forget, 1);
885900
rb_define_method(cState, "configure", cState_configure, 1);
886901
rb_define_method(cState, "to_h", cState_to_h, 0);
902+
rb_define_method(cState, "[]", cState_aref, 1);
887903

888904
mGeneratorMethods = rb_define_module_under(mGenerator, "GeneratorMethods");
889905
mObject = rb_define_module_under(mGeneratorMethods, "Object");
@@ -926,6 +942,9 @@ void Init_generator()
926942
i_create_id = rb_intern("create_id");
927943
i_extend = rb_intern("extend");
928944
i_key_p = rb_intern("key?");
945+
i_aref = rb_intern("[]");
946+
i_send = rb_intern("__send__");
947+
i_respond_to_p = rb_intern("respond_to?");
929948
#ifdef HAVE_RUBY_ENCODING_H
930949
mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
931950
i_encoding = rb_intern("encoding");

lib/json/pure/generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ def to_h
217217
end
218218
result
219219
end
220+
221+
# Return the value returned by method +name+.
222+
def [](name)
223+
__send__ name
224+
end
220225
end
221226

222227
module GeneratorMethods

lib/json/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module JSON
22
# JSON version
3-
VERSION = '1.2.2'
3+
VERSION = '1.2.3'
44
VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
55
VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
66
VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:

tests/test_json_generate.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ def test_states
8888
json = generate({1=>2}, nil)
8989
assert_equal('{"1":2}', json)
9090
s = JSON.state.new(:check_circular => true)
91-
#assert s.check_circular
91+
assert s.check_circular?
92+
assert s[:check_circular?]
9293
h = { 1=>2 }
9394
h[3] = h
9495
assert_raises(JSON::CircularDatastructure) { generate(h) }
9596
assert_raises(JSON::CircularDatastructure) { generate(h, s) }
9697
s = JSON.state.new(:check_circular => true)
97-
#assert s.check_circular
98+
assert s.check_circular?
99+
assert s[:check_circular?]
98100
a = [ 1, 2 ]
99101
a << a
100102
assert_raises(JSON::CircularDatastructure) { generate(a, s) }

0 commit comments

Comments
 (0)