Class: Psych::Pure::Emitter
- Inherits:
-
Object
- Object
- Psych::Pure::Emitter
- Defined in:
- lib/psych/pure.rb
Overview
The emitter is responsible for taking Ruby objects and converting them into YAML documents.
Direct Known Subclasses
Defined Under Namespace
Classes: AliasNode, ArrayNode, Formatter, HashNode, NilNode, Node, ObjectNode, OmapNode, SetNode, StringNode, Visitor
Instance Method Summary collapse
-
#emit(object) ⇒ Object
This is the main entrypoint into this object.
-
#initialize(io, options) ⇒ Emitter
constructor
Initialize a new emitter with the given io and options.
Constructor Details
#initialize(io, options) ⇒ Emitter
Initialize a new emitter with the given io and options.
4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 |
# File 'lib/psych/pure.rb', line 4590 def initialize(io, ) @io = io || $stdout = @started = false # These three instance variables are used to support dumping repeated # objects. When the same object is found more than once, we switch to # using an anchor and an alias. @object_nodes = {}.compare_by_identity @object_anchors = {}.compare_by_identity @object_anchor = 0 end |
Instance Method Details
#emit(object) ⇒ Object
This is the main entrypoint into this object. It is responsible for pushing a new object onto the emitter, which is then represented as a YAML document.
4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 |
# File 'lib/psych/pure.rb', line 4606 def emit(object) if @started @io << "...\n" else @started = true end # Very rare circumstance here that there are leading comments attached # to the root object of a document that occur before the --- marker. In # this case we want to output them first here, then dump the object. reload_comments = nil if (object.is_a?(LoadedObject) || object.is_a?(LoadedHash)) && (psych_node = object.psych_node).comments? && (leading = psych_node.comments.leading).any? leading = [*leading] line = psych_node.start_line - 1 while leading.any? && leading.last.start_line == line leading.pop line -= 1 end psych_node.comments.leading.slice!(0, leading.length) line = nil leading.each do |comment| if line && (line < comment.start_line) @io << "\n" * (comment.start_line - line - 1) end @io << comment.value @io << "\n" line = comment.start_line end reload_comments = leading.concat(psych_node.comments.leading) end @io << "---" if (node = dump(object)).is_a?(NilNode) @io << "\n" else q = Formatter.new(+"", 79, "\n") { |n| " " * n } if (node.is_a?(ArrayNode) || node.is_a?(HashNode)) && !node.value.empty? q.breakable else q.text(" ") end node.accept(Visitor.new(q, sequence_indent: .fetch(:sequence_indent, false))) q.breakable q.current_group.break q.flush @io << q.output end # If we initially split up the leading comments, then we need to reload # them back to their original state here. unless reload_comments.nil? object.psych_node.comments.leading.replace(reload_comments) end end |