diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 09:53:42 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2009-09-29 09:53:42 -0700 |
commit | d2bf942ac38b1b6e91116a06855a00d32b95d27b (patch) | |
tree | 0ecec2c190f7288929e0dbcfe2cc37547d5859cb | |
parent | a23403e809e8ddcb6b6cccf8f0169606cd376331 (diff) | |
download | psych-d2bf942ac38b1b6e91116a06855a00d32b95d27b.zip |
sequences emit
-rw-r--r-- | ext/psych/emitter.c | 39 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 8 | ||||
-rw-r--r-- | lib/psych/visitors/emitter.rb | 6 | ||||
-rw-r--r-- | test/visitors/test_emitter.rb | 15 |
4 files changed, 68 insertions, 0 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c index ce2b77e..7b8a43a 100644 --- a/ext/psych/emitter.c +++ b/ext/psych/emitter.c @@ -136,6 +136,43 @@ static VALUE scalar( return self; } +static VALUE start_sequence( + VALUE self, + VALUE anchor, + VALUE tag, + VALUE implicit, + VALUE style +) { + yaml_emitter_t * emitter; + Data_Get_Struct(self, yaml_emitter_t, emitter); + + yaml_event_t event; + yaml_sequence_start_event_initialize( + &event, + (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(anchor)), + (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(tag)), + Qtrue == implicit ? 1 : 0, + (yaml_sequence_style_t)NUM2INT(style) + ); + + emit(emitter, &event); + + return self; +} + +static VALUE end_sequence(VALUE self) +{ + yaml_emitter_t * emitter; + Data_Get_Struct(self, yaml_emitter_t, emitter); + + yaml_event_t event; + yaml_sequence_end_event_initialize(&event); + + emit(emitter, &event); + + return self; +} + void Init_psych_emitter() { VALUE psych = rb_define_module("Psych"); @@ -150,4 +187,6 @@ void Init_psych_emitter() rb_define_method(cPsychEmitter, "start_document", start_document, 3); rb_define_method(cPsychEmitter, "end_document", end_document, 1); rb_define_method(cPsychEmitter, "scalar", scalar, 6); + rb_define_method(cPsychEmitter, "start_sequence", start_sequence, 4); + rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0); } diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index b5ae131..a63fc60 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -1,3 +1,5 @@ +require 'stringio' + module Psych module Nodes ### @@ -15,6 +17,12 @@ module Psych def to_ruby Visitors::ToRuby.new.accept self end + + def to_yaml + io = StringIO.new + Visitors::Emitter.new(io).accept self + io.string + end end end end diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index e2dd0be..a0ff9d9 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -20,6 +20,12 @@ module Psych visitor_for(Nodes::Scalar) do |o| @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style end + + visitor_for(Nodes::Sequence) do |o| + @handler.start_sequence o.anchor, o.tag, o.implicit, o.style + o.children.each { |c| c.accept self } + @handler.end_sequence + end end end end diff --git a/test/visitors/test_emitter.rb b/test/visitors/test_emitter.rb index e2e14aa..b7b9110 100644 --- a/test/visitors/test_emitter.rb +++ b/test/visitors/test_emitter.rb @@ -26,6 +26,7 @@ module Psych @visitor.accept s assert_match(/1.1/, @io.string) + assert_equal @io.string, s.to_yaml end def test_scalar @@ -39,9 +40,23 @@ module Psych @visitor.accept s assert_match(/hello/, @io.string) + assert_equal @io.string, s.to_yaml end def test_sequence + s = Nodes::Stream.new + doc = Nodes::Document.new + scalar = Nodes::Scalar.new 'hello world' + seq = Nodes::Sequence.new + + seq.children << scalar + doc.children << seq + s.children << doc + + @visitor.accept s + + assert_match(/- hello/, @io.string) + assert_equal @io.string, s.to_yaml end end end |