W3cubDocs

/Crystal

class YAML::Nodes::Builder

Overview

Builds a tree of YAML nodes.

This builder is similar to YAML::Builder, but instead of directly emitting the output to an IO it builds a YAML document tree in memory.

All "emitting" methods support specifying a "reference" object that will be associated to the emitted object, so that when that reference object is emitted again an anchor and an alias will be created. This generates both more compact documents and allows handling recursive data structures.

Defined in:

yaml/nodes/builder.cr

Constructors

Instance Method Summary

Constructor Detail

def self.newSource

Instance Method Detail

def alias(anchor : String) : NilSource

Emits an alias to the given anchor.

require "yaml"

nodes_builder = YAML::Nodes::Builder.new

nodes_builder.mapping do
  nodes_builder.scalar "foo"
  nodes_builder.alias "key"
end

yaml = YAML.build do |builder|
  nodes_builder.document.to_yaml builder
end

yaml # => "---\nfoo: *key\n"

def document : DocumentSource

The document this builder builds.

def mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY, reference = nil, &) : NilSource

def merge(anchor : String) : NilSource

Emits the scalar "< followed by an alias to the given anchor.

See YAML Merge.

require "yaml"

nodes_builder = YAML::Nodes::Builder.new

nodes_builder.mapping do
  nodes_builder.merge "key"
end

yaml = YAML.build do |builder|
  nodes_builder.document.to_yaml builder
end

yaml # => "---\n<<: *key\n"

def scalar(value, anchor : String? = nil, tag : String? = nil, style : YAML::ScalarStyle = YAML::ScalarStyle::ANY, reference = nil) : NilSource

def sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY, reference = nil, &) : NilSource

© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/YAML/Nodes/Builder.html