summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych.rb1
-rw-r--r--lib/psych/ruby.rb7
-rw-r--r--lib/psych/visitors.rb1
-rw-r--r--lib/psych/visitors/yast_builder.rb28
-rw-r--r--test/visitors/test_yast_builder.rb15
5 files changed, 52 insertions, 0 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index e8f7ade..3ebc1fa 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -13,6 +13,7 @@ require 'psych/visitors'
require 'psych/handler'
require 'psych/tree_builder'
require 'psych/parser'
+require 'psych/ruby'
require 'psych/psych'
module Psych
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
new file mode 100644
index 0000000..6b454db
--- /dev/null
+++ b/lib/psych/ruby.rb
@@ -0,0 +1,7 @@
+class Object
+ include Psych::Visitable
+
+ def to_yaml options = {}
+ Psych::YASTBuilder.new(options).accept self
+ end
+end
diff --git a/lib/psych/visitors.rb b/lib/psych/visitors.rb
index df58e2e..0708e94 100644
--- a/lib/psych/visitors.rb
+++ b/lib/psych/visitors.rb
@@ -1,3 +1,4 @@
require 'psych/visitors/visitor'
require 'psych/visitors/to_ruby'
require 'psych/visitors/emitter'
+require 'psych/visitors/yast_builder'
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
new file mode 100644
index 0000000..65ed594
--- /dev/null
+++ b/lib/psych/visitors/yast_builder.rb
@@ -0,0 +1,28 @@
+module Psych
+ module Visitors
+ class YASTBuilder < Psych::Visitors::Visitor
+ attr_reader :tree
+
+ def initialize options = {}
+ super()
+ @tree = Nodes::Stream.new
+ @tree.children << Nodes::Document.new
+ @stack = @tree.children.dup
+ end
+
+ def accept target
+ target.class.ancestors.each do |klass|
+ method_name = :"visit_#{target.class.name.split('::').join('_')}"
+ if respond_to?(method_name)
+ return send(method_name, target)
+ end
+ end
+ raise "Can't handle #{target.class}"
+ end
+
+ visitor_for(::String) do |o|
+ @stack.last.children << Nodes::Scalar.new(o)
+ end
+ end
+ end
+end
diff --git a/test/visitors/test_yast_builder.rb b/test/visitors/test_yast_builder.rb
new file mode 100644
index 0000000..0e29f94
--- /dev/null
+++ b/test/visitors/test_yast_builder.rb
@@ -0,0 +1,15 @@
+require 'minitest/autorun'
+require 'psych'
+
+module Psych
+ module Visitors
+ class TestYASTBuilder < MiniTest::Unit::TestCase
+ def test_scalar
+ v = Visitors::YASTBuilder.new
+ v.accept 'foo'
+
+ assert_equal 'foo', Psych.load(v.tree.to_yaml)
+ end
+ end
+ end
+end