1require 'stringio'
2
3module Psych
4  module Nodes
5    ###
6    # The base class for any Node in a YAML parse tree.  This class should
7    # never be instantiated.
8    class Node
9      include Enumerable
10
11      # The children of this node
12      attr_reader :children
13
14      # An associated tag
15      attr_reader :tag
16
17      # Create a new Psych::Nodes::Node
18      def initialize
19        @children = []
20      end
21
22      ###
23      # Iterate over each node in the tree. Yields each node to +block+ depth
24      # first.
25      def each &block
26        return enum_for :each unless block_given?
27        Visitors::DepthFirst.new(block).accept self
28      end
29
30      ###
31      # Convert this node to Ruby.
32      #
33      # See also Psych::Visitors::ToRuby
34      def to_ruby
35        Visitors::ToRuby.new.accept self
36      end
37      alias :transform :to_ruby
38
39      ###
40      # Convert this node to YAML.
41      #
42      # See also Psych::Visitors::Emitter
43      def yaml io = nil, options = {}
44        real_io = io || StringIO.new(''.encode('utf-8'))
45
46        Visitors::Emitter.new(real_io, options).accept self
47        return real_io.string unless io
48        io
49      end
50      alias :to_yaml :yaml
51    end
52  end
53end
54