1module Psych
2  module Visitors
3    class Emitter < Psych::Visitors::Visitor
4      def initialize io, options = {}
5        opts = [:indentation, :canonical, :line_width].find_all { |opt|
6          options.key?(opt)
7        }
8
9        if opts.empty?
10          @handler = Psych::Emitter.new io
11        else
12          du = Handler::DumperOptions.new
13          opts.each { |option| du.send :"#{option}=", options[option] }
14          @handler = Psych::Emitter.new io, du
15        end
16      end
17
18      def visit_Psych_Nodes_Stream o
19        @handler.start_stream o.encoding
20        o.children.each { |c| accept c }
21        @handler.end_stream
22      end
23
24      def visit_Psych_Nodes_Document o
25        @handler.start_document o.version, o.tag_directives, o.implicit
26        o.children.each { |c| accept c }
27        @handler.end_document o.implicit_end
28      end
29
30      def visit_Psych_Nodes_Scalar o
31        @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style
32      end
33
34      def visit_Psych_Nodes_Sequence o
35        @handler.start_sequence o.anchor, o.tag, o.implicit, o.style
36        o.children.each { |c| accept c }
37        @handler.end_sequence
38      end
39
40      def visit_Psych_Nodes_Mapping o
41        @handler.start_mapping o.anchor, o.tag, o.implicit, o.style
42        o.children.each { |c| accept c }
43        @handler.end_mapping
44      end
45
46      def visit_Psych_Nodes_Alias o
47        @handler.alias o.anchor
48      end
49    end
50  end
51end
52