W3cubDocs

/Crystal

class YAML::Builder

Overview

A YAML builder generates valid YAML.

A YAML::Error is raised if attempting to generate an invalid YAML (for example, if invoking #end_sequence without a matching #start_sequence)

require "yaml"

string = YAML.build do |yaml|
  yaml.mapping do
    yaml.scalar "foo"
    yaml.sequence do
      yaml.scalar 1
      yaml.scalar 2
    end
    yaml.scalar "bar"
    yaml.mapping do
      yaml.scalar "baz"
      yaml.scalar "qux"
    end
  end
end
string # => "---\nfoo:\n- 1\n- 2\nbar:\n  baz: qux\n"

Defined in:

yaml/builder.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(io : IO)Source

Creates a YAML::Builder that will write to the given IO.

def self.new(io : IO, & : self -> ) : NilSource

Creates a YAML::Builder that writes to io and yields it to the block.

After returning from the block the builder is closed.

DEPRECATED Use .build instead

Class Method Detail

def self.build(io : IO, & : self -> ) : NilSource

Creates a YAML::Builder that writes to io and yields it to the block.

After returning from the block the builder is closed.

Instance Method Detail

def alias(anchor : String) : NilSource

Emits an alias to the given anchor.

require "yaml"

yaml = YAML.build do |builder|
  builder.mapping do
    builder.scalar "key"
    builder.alias "example"
  end
end

yaml # => "---\nkey: *example\n"

def closeSource

Closes the builder, freeing up resources.

def document(&)Source

Starts a document, invokes the block, and then ends it.

def end_documentSource

Ends a document.

def end_mappingSource

Ends a mapping.

def end_sequenceSource

Ends a sequence.

def end_streamSource

Ends a YAML stream.

def finalizeSource

def flushSource

Flushes any pending data to the underlying IO.

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

Starts a mapping, invokes the block, and then ends it.

def max_nesting : Int32Source

By default the maximum nesting of sequences/amppings is 99. Nesting more than this will result in a YAML::Error. Changing the value of this property allows more/less nesting.

def max_nesting=(max_nesting)Source

By default the maximum nesting of sequences/amppings is 99. Nesting more than this will result in a YAML::Error. Changing the value of this property allows more/less nesting.

def merge(anchor : String) : NilSource

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

See YAML Merge.

require "yaml"

yaml = YAML.build do |builder|
  builder.mapping do
    builder.merge "development"
  end
end

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

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

Emits a scalar value.

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

Starts a sequence, invokes the block, and the ends it.

def start_documentSource

Starts a document.

def start_mapping(anchor : String? = nil, tag : String? = nil, style : YAML::MappingStyle = YAML::MappingStyle::ANY)Source

Starts a mapping.

def start_sequence(anchor : String? = nil, tag : String? = nil, style : YAML::SequenceStyle = YAML::SequenceStyle::ANY)Source

Starts a sequence.

def start_streamSource

Starts a YAML stream.

def stream(&)Source

Starts a YAML stream, invokes the block, and ends it.

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