From ccc1107595545a7947080d919529102d3e8c6dfe Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 17 Nov 2009 19:47:29 -0800 Subject: renaming YASTBuilder -> YAMLTree --- lib/psych.rb | 2 +- lib/psych/visitors.rb | 2 +- lib/psych/visitors/yaml_tree.rb | 243 +++++++++++++++++++++++++++++++++++++ lib/psych/visitors/yast_builder.rb | 243 ------------------------------------- 4 files changed, 245 insertions(+), 245 deletions(-) create mode 100644 lib/psych/visitors/yaml_tree.rb delete mode 100644 lib/psych/visitors/yast_builder.rb (limited to 'lib') diff --git a/lib/psych.rb b/lib/psych.rb index df28ba0..74da20a 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -43,7 +43,7 @@ module Psych ### # Dump object +o+ to a YAML string def self.dump o, options = {} - visitor = Psych::Visitors::YASTBuilder.new options + visitor = Psych::Visitors::YAMLTree.new options visitor.accept o visitor.tree.to_yaml end diff --git a/lib/psych/visitors.rb b/lib/psych/visitors.rb index 0708e94..d00dcfd 100644 --- a/lib/psych/visitors.rb +++ b/lib/psych/visitors.rb @@ -1,4 +1,4 @@ require 'psych/visitors/visitor' require 'psych/visitors/to_ruby' require 'psych/visitors/emitter' -require 'psych/visitors/yast_builder' +require 'psych/visitors/yaml_tree' diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb new file mode 100644 index 0000000..0f0e3ba --- /dev/null +++ b/lib/psych/visitors/yaml_tree.rb @@ -0,0 +1,243 @@ +module Psych + module Visitors + class YAMLTree < Psych::Visitors::Visitor + attr_reader :tree + + def initialize options = {} + super() + @tree = Nodes::Stream.new + @tree.children << Nodes::Document.new + @stack = @tree.children.dup + @st = {} + end + + def accept target + target.class.ancestors.each do |klass| + next unless klass.name + method_name = :"visit_#{klass.name.split('::').join('_')}" + if respond_to?(method_name) + return send(method_name, target) + end + end + raise TypeError, "Can't dump #{target.class}" + end + + def visit_Object o + klass = o.class == Object ? nil : o.class.name + tag = ['!ruby/object', klass].compact.join(':') + @stack.push append Nodes::Mapping.new(nil, tag, false) + 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 + @stack.pop + end + + def visit_Struct o + tag = ['!ruby/struct', o.class.name].compact.join(':') + + @stack.push append Nodes::Mapping.new(nil, tag, false) + o.members.each do |member| + accept member + accept o[member] + end + + 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 + @stack.pop + end + + def visit_Exception o + tag = ['!ruby/exception', o.class.name].compact.join(':') + @stack.push append Nodes::Mapping.new(nil, tag, false) + + ['message', o.message].each do |m| + accept m + end + + 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 + + @stack.pop + end + + def visit_Regexp o + append Nodes::Scalar.new(o.inspect, nil, '!ruby/regexp', false) + end + + def visit_Time o + formatted = o.strftime("%Y-%m-%d %H:%M:%S") + if o.utc? + formatted += ".%06dZ" % [o.usec] + else + formatted += ".%06d %d:00" % [o.usec, o.gmt_offset / 3600] + end + + append Nodes::Scalar.new formatted + end + + def visit_Date o + append Nodes::Scalar.new o.to_s + end + + def visit_Rational o + @stack.push append Nodes::Mapping.new(nil,'!ruby/object:Rational',false) + ['denominator', o.denominator, 'numerator', o.numerator].each do |m| + accept m + end + @stack.pop + end + + def visit_Complex o + @stack.push append Nodes::Mapping.new(nil, '!ruby/object:Complex', false) + ['real', o.real, 'image', o.image].each do |m| + accept m + end + @stack.pop + end + + def visit_Integer o + append Nodes::Scalar.new o.to_s + end + + def visit_TrueClass o + append Nodes::Scalar.new o.to_s + end + + def visit_FalseClass o + append Nodes::Scalar.new o.to_s + end + + def visit_Float o + if o.nan? + append Nodes::Scalar.new '.nan' + elsif o.infinite? + append Nodes::Scalar.new(o.infinite? > 0 ? '.inf' : '-.inf') + else + append Nodes::Scalar.new o.to_s + end + end + + def visit_String o + 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 + + + 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 + raise TypeError, "can't dump anonymous class #{o.class}" + end + + def visit_Range o + @stack.push append Nodes::Mapping.new(nil, '!ruby/range', false) + ['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m| + accept m + end + @stack.pop + end + + def visit_Hash o + if node = @st[o.object_id] + node.anchor = o.object_id.to_s + return append Nodes::Alias.new o.object_id.to_s + end + + map = Nodes::Mapping.new + @st[o.object_id] = map + + @stack.push append map + + o.each do |k,v| + accept k + accept v + end + + @stack.pop + end + + def visit_Array o + if node = @st[o.object_id] + node.anchor = o.object_id.to_s + return append Nodes::Alias.new o.object_id.to_s + end + + seq = Nodes::Sequence.new + @st[o.object_id] = seq + + @stack.push append seq + o.each { |c| accept c } + @stack.pop + end + + def visit_NilClass o + append Nodes::Scalar.new('', nil, 'tag:yaml.org,2002:null', false) + end + + def visit_Symbol o + append Nodes::Scalar.new ":#{o}" + end + + private + def append o + @stack.last.children << o + o + end + end + end +end diff --git a/lib/psych/visitors/yast_builder.rb b/lib/psych/visitors/yast_builder.rb deleted file mode 100644 index 4eb21fd..0000000 --- a/lib/psych/visitors/yast_builder.rb +++ /dev/null @@ -1,243 +0,0 @@ -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 - @st = {} - end - - def accept target - target.class.ancestors.each do |klass| - next unless klass.name - method_name = :"visit_#{klass.name.split('::').join('_')}" - if respond_to?(method_name) - return send(method_name, target) - end - end - raise TypeError, "Can't dump #{target.class}" - end - - def visit_Object o - klass = o.class == Object ? nil : o.class.name - tag = ['!ruby/object', klass].compact.join(':') - @stack.push append Nodes::Mapping.new(nil, tag, false) - 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 - @stack.pop - end - - def visit_Struct o - tag = ['!ruby/struct', o.class.name].compact.join(':') - - @stack.push append Nodes::Mapping.new(nil, tag, false) - o.members.each do |member| - accept member - accept o[member] - end - - 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 - @stack.pop - end - - def visit_Exception o - tag = ['!ruby/exception', o.class.name].compact.join(':') - @stack.push append Nodes::Mapping.new(nil, tag, false) - - ['message', o.message].each do |m| - accept m - end - - 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 - - @stack.pop - end - - def visit_Regexp o - append Nodes::Scalar.new(o.inspect, nil, '!ruby/regexp', false) - end - - def visit_Time o - formatted = o.strftime("%Y-%m-%d %H:%M:%S") - if o.utc? - formatted += ".%06dZ" % [o.usec] - else - formatted += ".%06d %d:00" % [o.usec, o.gmt_offset / 3600] - end - - append Nodes::Scalar.new formatted - end - - def visit_Date o - append Nodes::Scalar.new o.to_s - end - - def visit_Rational o - @stack.push append Nodes::Mapping.new(nil,'!ruby/object:Rational',false) - ['denominator', o.denominator, 'numerator', o.numerator].each do |m| - accept m - end - @stack.pop - end - - def visit_Complex o - @stack.push append Nodes::Mapping.new(nil, '!ruby/object:Complex', false) - ['real', o.real, 'image', o.image].each do |m| - accept m - end - @stack.pop - end - - def visit_Integer o - append Nodes::Scalar.new o.to_s - end - - def visit_TrueClass o - append Nodes::Scalar.new o.to_s - end - - def visit_FalseClass o - append Nodes::Scalar.new o.to_s - end - - def visit_Float o - if o.nan? - append Nodes::Scalar.new '.nan' - elsif o.infinite? - append Nodes::Scalar.new(o.infinite? > 0 ? '.inf' : '-.inf') - else - append Nodes::Scalar.new o.to_s - end - end - - def visit_String o - 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 - - - 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 - raise TypeError, "can't dump anonymous class #{o.class}" - end - - def visit_Range o - @stack.push append Nodes::Mapping.new(nil, '!ruby/range', false) - ['begin', o.begin, 'end', o.end, 'excl', o.exclude_end?].each do |m| - accept m - end - @stack.pop - end - - def visit_Hash o - if node = @st[o.object_id] - node.anchor = o.object_id.to_s - return append Nodes::Alias.new o.object_id.to_s - end - - map = Nodes::Mapping.new - @st[o.object_id] = map - - @stack.push append map - - o.each do |k,v| - accept k - accept v - end - - @stack.pop - end - - def visit_Array o - if node = @st[o.object_id] - node.anchor = o.object_id.to_s - return append Nodes::Alias.new o.object_id.to_s - end - - seq = Nodes::Sequence.new - @st[o.object_id] = seq - - @stack.push append seq - o.each { |c| accept c } - @stack.pop - end - - def visit_NilClass o - append Nodes::Scalar.new('', nil, 'tag:yaml.org,2002:null', false) - end - - def visit_Symbol o - append Nodes::Scalar.new ":#{o}" - end - - private - def append o - @stack.last.children << o - o - end - end - end -end -- cgit v1.2.3