Class: Terraspace::Compiler::Strategy::Tfvar

Inherits:
Object
  • Object
show all
Defined in:
lib/terraspace/compiler/strategy/tfvar.rb,
lib/terraspace/compiler/strategy/tfvar/rb.rb,
lib/terraspace/compiler/strategy/tfvar/base.rb,
lib/terraspace/compiler/strategy/tfvar/layer.rb,
lib/terraspace/compiler/strategy/tfvar/tfvars.rb

Defined Under Namespace

Classes: Base, Layer, Rb, Tfvars

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Tfvar

Returns a new instance of Tfvar.



3
4
5
6
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 3

def initialize(mod)
  @mod = mod
  @order = 0
end

Instance Method Details

#layer_pathsObject



33
34
35
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 33

def layer_paths
  Layer.new(@mod).paths
end

#ordered_name(layer_path) ⇒ Object

Tact on number to ensure that tfvars will be processed in desired order. Also name auto.tfvars so it will automatically load



25
26
27
28
29
30
31
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 25

def ordered_name(layer_path)
  @order += 1
  prefix = @order.to_s.rjust(2, '0') # add leading 0 in case there are more than 10 layers
  name = "#{prefix}-#{File.basename(layer_path)}"
  name.sub('.tfvars','.auto.tfvars')
      .sub('.rb','.auto.tfvars.json')
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 8

def run
  layer_paths.each do |layer_path|
    ext = File.extname(layer_path).sub('.','')
    klass = strategy_class(ext)
    next unless klass

    strategy = klass.new(@mod, layer_path)
    content = strategy.run

    dest_name = ordered_name(layer_path)
    writer = Terraspace::Compiler::Writer.new(@mod, dest_name: dest_name)
    writer.write(content)
  end
end

#strategy_class(ext) ⇒ Object



37
38
39
40
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 37

def strategy_class(ext)
  "Terraspace::Compiler::Strategy::Tfvar::#{ext.camelize}".constantize
rescue NameError
end