Sha256: c8bb3f69e8807c307687152834addd08fb3662181d095e4e61232df5b5a45d29

Contents?: true

Size: 1019 Bytes

Versions: 104

Compression:

Stored size: 1019 Bytes

Contents

module Terraspace::Compiler::Dsl
  class Tfvars < Base
    include Terraspace::Compiler::Dsl::Syntax::Tfvar

    # Can return nil if there's no tfvars declared
    def build
      evaluate(@src_path)
      result = @structure.deep_stringify_keys
      JSON.pretty_generate(result) unless result.empty?
    end

    def evaluate(path)
      current_instance_vars = instance_variables
      evaluate_file(path) # tfvar calls creates variables in @structure
      new_instance_vars = instance_variables - current_instance_vars
      add_instance_vars!(new_instance_vars)
    end

    def add_instance_vars!(new_instance_vars)
      # IE: new_instance_variables = [:@cidr_block, :@name]
      @structure.deep_stringify_keys!
      new_instance_vars.each do |var|
        key = var.to_s.sub('@','') # better to keep String as tfvar will usually use String notation, also deep_stringify_keys! earlier just in case
        value = instance_variable_get(var)
        @structure.merge!(key => value)
      end
    end
  end
end

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.16 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.15 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.14 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.13 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.12 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.11 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.10 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.9 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.8 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.7 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.6 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.5 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.4 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.3 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.2 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.1 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.2.0 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.1.7 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-2.1.6 lib/terraspace/compiler/dsl/tfvars.rb