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-0.5.3 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.5.2 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.5.1 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.5.0 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.4.4 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.4.3 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.4.2 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.4.1 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.4.0 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.6 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.5 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.4 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.3 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.2 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.1 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.3.0 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.2.4 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.2.3 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.2.2 lib/terraspace/compiler/dsl/tfvars.rb
terraspace-0.2.1 lib/terraspace/compiler/dsl/tfvars.rb