summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/psych/emitter.c17
-rw-r--r--lib/psych/visitors/emitter.rb4
-rw-r--r--test/visitors/test_emitter.rb15
3 files changed, 36 insertions, 0 deletions
diff --git a/ext/psych/emitter.c b/ext/psych/emitter.c
index d6b13f9..126f174 100644
--- a/ext/psych/emitter.c
+++ b/ext/psych/emitter.c
@@ -210,6 +210,22 @@ static VALUE end_mapping(VALUE self)
return self;
}
+static VALUE alias(VALUE self, VALUE anchor)
+{
+ yaml_emitter_t * emitter;
+ Data_Get_Struct(self, yaml_emitter_t, emitter);
+
+ yaml_event_t event;
+ yaml_alias_event_initialize(
+ &event,
+ (yaml_char_t *)(Qnil == anchor ? NULL : StringValuePtr(anchor))
+ );
+
+ emit(emitter, &event);
+
+ return self;
+}
+
void Init_psych_emitter()
{
VALUE psych = rb_define_module("Psych");
@@ -228,4 +244,5 @@ void Init_psych_emitter()
rb_define_method(cPsychEmitter, "end_sequence", end_sequence, 0);
rb_define_method(cPsychEmitter, "start_mapping", start_mapping, 4);
rb_define_method(cPsychEmitter, "end_mapping", end_mapping, 0);
+ rb_define_method(cPsychEmitter, "alias", alias, 1);
}
diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb
index 966a09e..3e33095 100644
--- a/lib/psych/visitors/emitter.rb
+++ b/lib/psych/visitors/emitter.rb
@@ -32,6 +32,10 @@ module Psych
o.children.each { |c| c.accept self }
@handler.end_mapping
end
+
+ visitor_for(Nodes::Alias) do |o|
+ @handler.alias o.anchor
+ end
end
end
end
diff --git a/test/visitors/test_emitter.rb b/test/visitors/test_emitter.rb
index 6f19df9..e79a7b5 100644
--- a/test/visitors/test_emitter.rb
+++ b/test/visitors/test_emitter.rb
@@ -89,6 +89,21 @@ module Psych
assert_match(/key: value/, @io.string)
assert_equal @io.string, s.to_yaml
end
+
+ def test_alias
+ s = Nodes::Stream.new
+ doc = Nodes::Document.new
+ mapping = Nodes::Mapping.new
+ mapping.children << Nodes::Scalar.new('key', 'A')
+ mapping.children << Nodes::Alias.new('A')
+ doc.children << mapping
+ s.children << doc
+
+ @visitor.accept s
+
+ assert_match(/&A key: \*A/, @io.string)
+ assert_equal @io.string, s.to_yaml
+ end
end
end
end