From a2e3de1b3f876dd9b14fa3a6291a202719405382 Mon Sep 17 00:00:00 2001 From: tenderlove Date: Thu, 8 Mar 2012 21:21:52 +0000 Subject: * ext/psych/lib/psych.rb (parse_stream, load_stream): if a block is given, documents will be yielded to the block as they are parsed. [ruby-core:42404] [Bug #5978] * ext/psych/lib/psych/handlers/document_stream.rb: add a handler that yields documents as they are parsed * test/psych/test_stream.rb: corresponding tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34953 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/psych/lib/psych.rb | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'ext/psych/lib/psych.rb') diff --git a/ext/psych/lib/psych.rb b/ext/psych/lib/psych.rb index cb5c4a5cf3..69f3a7c1fe 100644 --- a/ext/psych/lib/psych.rb +++ b/ext/psych/lib/psych.rb @@ -13,6 +13,7 @@ require 'psych/deprecated' require 'psych/stream' require 'psych/json/tree_builder' require 'psych/json/stream' +require 'psych/handlers/document_stream' ### # = Overview @@ -173,12 +174,19 @@ module Psych # +filename+ is used in the exception message if a Psych::SyntaxError is # raised. # + # If a block is given, a Psych::Nodes::Document node will be yielded to the + # block as it's being parsed. + # # Raises a Psych::SyntaxError when a YAML syntax error is detected. # # Example: # # Psych.parse_stream("---\n - a\n - b") # => # # + # Psych.parse_stream("--- a\n--- b") do |node| + # node # => # + # end + # # begin # Psych.parse_stream("--- `", "file.txt") # rescue Psych::SyntaxError => ex @@ -187,10 +195,15 @@ module Psych # end # # See Psych::Nodes for more information about YAML AST. - def self.parse_stream yaml, filename = nil - parser = self.parser - parser.parse yaml, filename - parser.handler.root + def self.parse_stream yaml, filename = nil, &block + if block_given? + parser = Psych::Parser.new(Handlers::DocumentStream.new(&block)) + parser.parse yaml, filename + else + parser = self.parser + parser.parse yaml, filename + parser.handler.root + end end ### @@ -252,12 +265,27 @@ module Psych ### # Load multiple documents given in +yaml+. Returns the parsed documents - # as a list. For example: + # as a list. If a block is given, each document will be converted to ruby + # and passed to the block during parsing + # + # Example: # # Psych.load_stream("--- foo\n...\n--- bar\n...") # => ['foo', 'bar'] # + # list = [] + # Psych.load_stream("--- foo\n...\n--- bar\n...") do |ruby| + # list << ruby + # end + # list # => ['foo', 'bar'] + # def self.load_stream yaml, filename = nil - parse_stream(yaml, filename).children.map { |child| child.to_ruby } + if block_given? + parse_stream(yaml, filename) do |node| + yield node.to_ruby + end + else + parse_stream(yaml, filename).children.map { |child| child.to_ruby } + end end ### -- cgit v1.2.3