HCL::Builder
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}"
Constructors
Instantiate's a new HCL::Builder with the given root node. This
generally does not need to be invoked directly.
Class methods
Yields an HCL::Builder instance for the given root node, which defaults to
HCL::AST::Document. Returns the HCL::Builder instance.
Instance methods
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::Number node with the given value
Raises if value cannot be used or converted to a supported number format