1# frozen_string_literal: true 2require 'stringio' 3require 'psych/class_loader' 4require 'psych/scalar_scanner' 5 6module Psych 7 module Nodes 8 ### 9 # The base class for any Node in a YAML parse tree. This class should 10 # never be instantiated. 11 class Node 12 include Enumerable 13 14 # The children of this node 15 attr_reader :children 16 17 # An associated tag 18 attr_reader :tag 19 20 # The line number where this node start 21 attr_accessor :start_line 22 23 # The column number where this node start 24 attr_accessor :start_column 25 26 # The line number where this node ends 27 attr_accessor :end_line 28 29 # The column number where this node ends 30 attr_accessor :end_column 31 32 # Create a new Psych::Nodes::Node 33 def initialize 34 @children = [] 35 end 36 37 ### 38 # Iterate over each node in the tree. Yields each node to +block+ depth 39 # first. 40 def each &block 41 return enum_for :each unless block_given? 42 Visitors::DepthFirst.new(block).accept self 43 end 44 45 ### 46 # Convert this node to Ruby. 47 # 48 # See also Psych::Visitors::ToRuby 49 def to_ruby 50 Visitors::ToRuby.create.accept(self) 51 end 52 alias :transform :to_ruby 53 54 ### 55 # Convert this node to YAML. 56 # 57 # See also Psych::Visitors::Emitter 58 def yaml io = nil, options = {} 59 real_io = io || StringIO.new(''.encode('utf-8')) 60 61 Visitors::Emitter.new(real_io, options).accept self 62 return real_io.string unless io 63 io 64 end 65 alias :to_yaml :yaml 66 67 def alias?; false; end 68 def document?; false; end 69 def mapping?; false; end 70 def scalar?; false; end 71 def sequence?; false; end 72 def stream?; false; end 73 end 74 end 75end 76