class

HCL::Builder

Inherits Reference / Object

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

new(node : AST::Node)

Instantiate's a new HCL::Builder with the given root node. This generally does not need to be invoked directly.

Source

Class methods

build(node : AST::Node | Nil = AST::Document.new, &)

Yields an HCL::Builder instance for the given root node, which defaults to HCL::AST::Document. Returns the HCL::Builder instance.

Source

Instance methods

<<(value : AST::Node)

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.

Source
<<(value)

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.

Source
attribute(name, &)

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.

Source
block(name, *args, &)

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)

Source
identifier(value)

Returns a new AST::Identifier node for the given value.

Source
label(value : Symbol | String)

Appends a new AST::BlockLabel node with the given value to the block's labels collection.

Raises if active node is not a block.

Source
list

Yields a new HCL::Builder for building a list.

Source
literal(value : Bool)

Returns a new AST::Literal node for the given boolean value.

Source
literal(value)

Returns a new AST::Literal node for the given value.

Source
map

Yields a new HCL::Builder for building a map/object.

Source
node
Source
number(value)

Returns a new AST::Number node with the given value

Raises if value cannot be used or converted to a supported number format

Source
to_hcl(_builder : HCL::Builder)

Returns the HCL::AST::Node for the builder.

Source
to_hcl(io : IO)

Writes a string representation of the HCL node to the given IO.

Source
to_s(io : IO)

Alias for #to_hcl(io : IO).

Source