summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-03-25 21:46:07 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-03-25 21:46:07 -0700
commitc95160bc7793f51c889b5f23cbb547923047450a (patch)
treed5e6897408919a29987deac1d339d454a8639932 /lib
parent8be32c99e14b9d5cbb6499411c58c41558a0ef31 (diff)
downloadpsych-c95160bc7793f51c889b5f23cbb547923047450a.zip
adding dump stream and load stream
Diffstat (limited to 'lib')
-rw-r--r--lib/psych.rb34
-rw-r--r--lib/psych/visitors/yaml_tree.rb18
2 files changed, 35 insertions, 17 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index e53ced2..1cdca80 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -145,11 +145,6 @@ module Psych
parser.handler.root
end
- def self.load_stream yaml # :nodoc:
- warn "#{caller[0]}: load_stream is deprecated, use parse_stream"
- parse_stream yaml
- end
-
###
# Dump Ruby object +o+ to a YAML string using +options+.
#
@@ -158,7 +153,21 @@ module Psych
# Psych.dump(['a', 'b']) # => "---\n- a\n- b\n"
def self.dump o, options = {}
visitor = Psych::Visitors::YAMLTree.new options
- visitor.accept o
+ visitor << o
+ visitor.tree.to_yaml
+ end
+
+ ###
+ # Dump a list of objects as separate documents to a document stream.
+ #
+ # Example:
+ #
+ # Psych.dump_stream("foo\n ", {}) # => "--- ! \"foo\\n \"\n--- {}\n"
+ def self.dump_stream *objects
+ visitor = Psych::Visitors::YAMLTree.new {}
+ objects.each do |o|
+ visitor << o
+ end
visitor.tree.to_yaml
end
@@ -166,7 +175,7 @@ module Psych
# Dump Ruby object +o+ to a JSON string.
def self.to_json o
visitor = Psych::Visitors::JSONTree.new(:json => true)
- visitor.accept o
+ visitor << o
visitor.tree.to_yaml
end
@@ -176,13 +185,16 @@ module Psych
#
# Psych.load_documents("--- foo\n...\n--- bar\n...") # => ['foo', 'bar']
#
- def self.load_documents yaml, &block
- list = parse_stream(yaml).children.map { |child| child.to_ruby }
+ def self.load_stream yaml
+ parse_stream(yaml).children.map { |child| child.to_ruby }
+ end
- return list unless block_given?
+ def self.load_documents yaml, &block
if $VERBOSE
- warn "#{caller[0]}: calling load_documents with a block is deprecated"
+ warn "#{caller[0]}: load_documents is deprecated, use load_stream"
end
+ list = load_stream yaml
+ return list unless block_given?
list.each(&block)
end
diff --git a/lib/psych/visitors/yaml_tree.rb b/lib/psych/visitors/yaml_tree.rb
index 523cfe1..e1e89a0 100644
--- a/lib/psych/visitors/yaml_tree.rb
+++ b/lib/psych/visitors/yaml_tree.rb
@@ -5,12 +5,11 @@ module Psych
def initialize options = {}
super()
- @json = options[:json]
- @tree = Nodes::Stream.new
- @tree.children << create_document
- @stack = @tree.children.dup
- @st = {}
- @ss = ScalarScanner.new
+ @json = options[:json]
+ @tree = Nodes::Stream.new
+ @stack = []
+ @st = {}
+ @ss = ScalarScanner.new
@dispatch_cache = Hash.new do |h,klass|
method = "visit_#{(klass.name || '').split('::').join('_')}"
@@ -23,6 +22,13 @@ module Psych
end
end
+ def << object
+ doc = create_document
+ @stack << doc
+ @tree.children << doc
+ accept object
+ end
+
def accept target
# return any aliases we find
if node = @st[target.object_id]