From 6faec131cb140948003fff0a89064f737e3b1b4d Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 9 Apr 2010 08:36:24 -0700 Subject: merging from ruby to psych --- lib/psych.rb | 11 ++++++++--- lib/psych/coder.rb | 11 ++++++++++- lib/psych/core_ext.rb | 5 ++++- lib/psych/nodes/node.rb | 10 ++++++---- 4 files changed, 28 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/psych.rb b/lib/psych.rb index c142593..9b4b215 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -1,4 +1,4 @@ -require 'psych/psych' +require 'psych.so' require 'psych/nodes' require 'psych/visitors' require 'psych/handler' @@ -153,10 +153,15 @@ module Psych # Example: # # Psych.dump(['a', 'b']) # => "---\n- a\n- b\n" - def self.dump o, options = {} + def self.dump o, io = nil, options = {} + if Hash === io + options = io + io = nil + end + visitor = Psych::Visitors::YAMLTree.new options visitor << o - visitor.tree.to_yaml + visitor.tree.to_yaml io end ### diff --git a/lib/psych/coder.rb b/lib/psych/coder.rb index 79e46da..eff0cc3 100644 --- a/lib/psych/coder.rb +++ b/lib/psych/coder.rb @@ -7,7 +7,7 @@ module Psych # called, respectively. class Coder attr_accessor :tag, :style, :implicit - attr_reader :type, :map, :scalar, :seq + attr_reader :type, :scalar, :seq def initialize tag @map = {} @@ -19,6 +19,14 @@ module Psych @scalar = nil end + # Emit a map. The coder will be yielded to the block. + def map tag = @tag, style = @style + @tag = tag + @style = style + yield self if block_given? + @map + end + # Emit a scalar with +value+ and +tag+ def represent_scalar tag, value self.tag = tag @@ -53,6 +61,7 @@ module Psych @type = :map @map[k] = v end + alias :add :[]= def [] k @type = :map diff --git a/lib/psych/core_ext.rb b/lib/psych/core_ext.rb index bd22219..9c55c70 100644 --- a/lib/psych/core_ext.rb +++ b/lib/psych/core_ext.rb @@ -12,11 +12,14 @@ class Object def psych_to_yaml options = {} Psych.dump self, options end + remove_method :to_yaml rescue nil alias :to_yaml :psych_to_yaml end module Kernel - def y *objects + def psych_y *objects puts Psych.dump_stream(*objects) end + remove_method :y rescue nil + alias y psych_y end diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index 0de768c..3ab9aca 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -30,10 +30,12 @@ module Psych # Convert this node to YAML. # # See also Psych::Visitors::Emitter - def to_yaml - io = StringIO.new - Visitors::Emitter.new(io).accept self - io.string + def to_yaml io = nil + real_io = io || StringIO.new + + Visitors::Emitter.new(real_io).accept self + return real_io.string unless io + io end end end -- cgit v1.2.3