summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/psych.rb6
-rw-r--r--lib/psych/ruby.rb4
-rw-r--r--lib/psych/visitors/to_ruby.rb8
-rw-r--r--lib/psych/visitors/yast_builder.rb56
4 files changed, 64 insertions, 10 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index 9250a2c..df28ba0 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -42,8 +42,10 @@ module Psych
###
# Dump object +o+ to a YAML string
- def self.dump o
- o.to_yaml
+ def self.dump o, options = {}
+ visitor = Psych::Visitors::YASTBuilder.new options
+ visitor.accept o
+ visitor.tree.to_yaml
end
###
diff --git a/lib/psych/ruby.rb b/lib/psych/ruby.rb
index f564565..47e4421 100644
--- a/lib/psych/ruby.rb
+++ b/lib/psych/ruby.rb
@@ -13,8 +13,6 @@ class Object
include Psych::Visitable
def to_yaml options = {}
- visitor = Psych::Visitors::YASTBuilder.new options
- visitor.accept self
- visitor.tree.to_yaml
+ Psych.dump self, options
end
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index dd8805b..f91537f 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -75,6 +75,14 @@ module Psych
def visit_Psych_Nodes_Mapping o
case o.tag
+ when '!str', 'tag:yaml.org,2002:str'
+ members = Hash[*o.children.map { |c| accept c }]
+ string = members.delete 'str'
+
+ members.each do |k,v|
+ string.instance_variable_set k, v
+ end
+ string
when /!ruby\/struct:?(.*)?$/
klassname = $1
members = o.children.map { |c| accept c }
diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb
index 6c5f25f..6e2295c 100644
--- a/lib/psych/visitors/yast_builder.rb
+++ b/lib/psych/visitors/yast_builder.rb
@@ -26,7 +26,13 @@ module Psych
klass = o.class == Object ? nil : o.class.name
tag = ['!ruby/object', klass].compact.join(':')
@stack.push append Nodes::Mapping.new(nil, tag, false)
- o.instance_variables.each do |iv|
+ if o.respond_to? :to_yaml_properties
+ ivars = o.to_yaml_properties
+ else
+ ivars = o.instance_variables
+ end
+
+ ivars.each do |iv|
accept iv.to_s.sub(/^@/, '')
accept o.instance_variable_get(iv)
end
@@ -41,7 +47,14 @@ module Psych
accept member
accept o[member]
end
- o.instance_variables.each do |iv|
+
+ if o.respond_to? :to_yaml_properties
+ ivars = o.to_yaml_properties
+ else
+ ivars = o.instance_variables
+ end
+
+ ivars.each do |iv|
accept iv.to_s.sub(/^@/, '')
accept o.instance_variable_get(iv)
end
@@ -114,10 +127,43 @@ module Psych
end
def visit_String o
- quote = ScalarScanner.new(o).tokenize.first != :SCALAR
+ plain = false
+ quote = false
+
+ if o.index("\x00") || o.count("^ -~\t\r\n").fdiv(o.length) > 0.3
+ str = [o].pack('m').chomp
+ tag = '!binary'
+ else
+ str = o
+ tag = nil
+ quote = ScalarScanner.new(o).tokenize.first != :SCALAR
+ plain = !quote
+ end
- scalar = Nodes::Scalar.new(o, nil, nil, !quote, quote)
- @stack.last.children << scalar
+
+ if o.respond_to? :to_yaml_properties
+ ivars = o.to_yaml_properties
+ else
+ ivars = o.instance_variables
+ end
+
+ scalar = Nodes::Scalar.new str, nil, tag, plain, quote
+
+ if ivars.empty?
+ append scalar
+ else
+ mapping = append Nodes::Mapping.new(nil, '!str', false)
+
+ mapping.children << Nodes::Scalar.new('str')
+ mapping.children << scalar
+
+ @stack.push mapping
+ ivars.each do |iv|
+ mapping.children << Nodes::Scalar.new(":#{iv}")
+ accept o.instance_variable_get(iv)
+ end
+ @stack.pop
+ end
end
def visit_Class o