class HCL::Builder
Overview
An HCL builder helps build a valid HCL abstract syntax tree.
An HCL::BuildError
will be raised on any attempts to create an AST
that would be invalid (e.g. setting an attribute's value to a block)
require "hcl"
string = HCL.build do |hcl|
hcl.block "aws_instance", "c4.xlarge" do |blk|
blk.attribute "security_group_ids" do
blk.list do |l|
l << l.literal("sg-123")
l << l.literal("sg-456")
end
end
blk.attribute("region") { "us-west-2" }
blk.block("tags") do |t|
t.attribute("name") { "test" }
end
end
end
string # => "aws_instance \"c4.xlarge\" {\n security_group_ids = [\"sg-123\", \"sg-456\"]\n region = \"us-west-2\"\n tags {\n name = \"test\"\m}"
Defined in:
hcl/builder.crConstructors
-
.new(node : AST::Node)
Instantiate's a new
HCL::Builder
with the given root node.
Class Method Summary
-
.build(node : AST::Node | Nil = AST::Document.new, &)
Yields an
HCL::Builder
instance for the given root node, which defaults toHCL::AST::Document
.
Instance Method Summary
-
#<<(value : AST::Node)
Appends an
HCL::AST::Node
to the underlying list node. -
#<<(value)
Appends a value to the the underlying list node.
-
#attribute(name, &)
Adds an attribute to the open HCL body or map/object with the value of the passed in block.
-
#block(name, *args, &)
Yields a new
HCL::Builder
for building a block. -
#identifier(value)
Returns a new
AST::Identifier
node for the given value. -
#label(value : Symbol | String)
Appends a new
AST::BlockLabel
node with the given value to the block's labels collection. -
#list(&)
Yields a new
HCL::Builder
for building a list. -
#literal(value : Bool)
Returns a new
AST::Literal
node for the given boolean value. -
#literal(value)
Returns a new
AST::Literal
node for the given value. -
#map(&)
Yields a new
HCL::Builder
for building a map/object. - #node : HCL::AST::Node
-
#number(value)
Returns a new
AST::Number
node with the given value -
#to_hcl(_builder : HCL::Builder)
Returns the
HCL::AST::Node
for the builder. -
#to_hcl(io : IO)
Writes a string representation of the HCL node to the given IO.
-
#to_s(io : IO)
Alias for
#to_hcl(io : IO)
.
Instance methods inherited from class Reference
==(other : HCL::Any)
==
Instance methods inherited from class Object
===(other : HCL::Any)
===
Class methods inherited from class Object
from_hcl(string_or_io : String | IO, ctx : HCL::ExpressionContext = HCL::ExpressionContext.default_context)
from_hcl
Constructor Detail
Instantiate's a new HCL::Builder
with the given root node. This
generally does not need to be invoked directly.
Class Method Detail
Yields an HCL::Builder
instance for the given root node, which defaults to
HCL::AST::Document
. Returns the HCL::Builder
instance.
Instance Method Detail
Appends an HCL::AST::Node
to the underlying list node. Raises if the builder is not
for a list or if the node is not usable within a list.
Appends a value to the the underlying list node. Value most be convertable
to HCL, either through base types supported by the library or a
#to_hcl(builder : HCL::Builder)
method on the object.
Raises if the builder is not for a list or if the node is not usable within a list.
Adds an attribute to the open HCL body or map/object with the value of the
passed in block. Value must by an AST node or an object convertable to HCL,
either through base types supported by the library or a
#to_hcl(builder : HCL::Builder)
method on the object.
Yields a new HCL::Builder
for building a block. Adds the block to the
open HCL body. name
is the first parameter, but subsequent parameters are
used as label values on the block. #label
may also be used within the
block to set labels.
Raises if used within a non-body node (i.e. not a document or block)
Appends a new AST::BlockLabel
node with the given value to the block's
labels collection.
Raises if active node is not a block.
Returns a new AST::Literal
node for the given boolean value.
Returns a new AST::Number
node with the given value
Raises if value cannot be used or converted to a supported number format