diff options
Diffstat (limited to 'ruby_1_9_3/ext/psych/lib/psych/nodes/node.rb')
-rw-r--r-- | ruby_1_9_3/ext/psych/lib/psych/nodes/node.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/ruby_1_9_3/ext/psych/lib/psych/nodes/node.rb b/ruby_1_9_3/ext/psych/lib/psych/nodes/node.rb new file mode 100644 index 0000000000..0cefe44e44 --- /dev/null +++ b/ruby_1_9_3/ext/psych/lib/psych/nodes/node.rb @@ -0,0 +1,53 @@ +require 'stringio' + +module Psych + module Nodes + ### + # The base class for any Node in a YAML parse tree. This class should + # never be instantiated. + class Node + include Enumerable + + # The children of this node + attr_reader :children + + # An associated tag + attr_reader :tag + + # Create a new Psych::Nodes::Node + def initialize + @children = [] + end + + ### + # Iterate over each node in the tree. Yields each node to +block+ depth + # first. + def each &block + return enum_for :each unless block_given? + Visitors::DepthFirst.new(block).accept self + end + + ### + # Convert this node to Ruby. + # + # See also Psych::Visitors::ToRuby + def to_ruby + Visitors::ToRuby.new.accept self + end + alias :transform :to_ruby + + ### + # Convert this node to YAML. + # + # See also Psych::Visitors::Emitter + def yaml io = nil, options = {} + real_io = io || StringIO.new(''.encode('utf-8')) + + Visitors::Emitter.new(real_io, options).accept self + return real_io.string unless io + io + end + alias :to_yaml :yaml + end + end +end |