Class: Orchparty::Transformations::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/orchparty/transformations/variable.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Variable

Returns a new instance of Variable.



6
7
8
# File 'lib/orchparty/transformations/variable.rb', line 6

def initialize(opts = {})
  @force_variable_definition = opts[:force_variable_definition]
end

Instance Method Details

#build_context(application:, service:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/orchparty/transformations/variable.rb', line 44

def build_context(application:, service:)
  variables = application._variables || {}
  variables = variables.merge({ application: application.merge(application._variables) })
  if service
    variables = variables.merge(service._variables)
    variables = variables.merge({ service: service.merge(service._variables) })
  end
  context = Context.new(variables)
  context._force_variable_definition = @force_variable_definition
  context
end

#eval_value(context, value) ⇒ Object



40
41
42
# File 'lib/orchparty/transformations/variable.rb', line 40

def eval_value(context, value)
  context.instance_exec(&value)
end

#resolve(application, subject, service) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/orchparty/transformations/variable.rb', line 22

def resolve(application, subject, service)
  subject.deep_transform_values! do |v|
    if v.respond_to?(:call)
      eval_value(build_context(application:, service:), v)
    elsif v.is_a? Array
      v.map do |v|
        if v.respond_to?(:call)
          eval_value(build_context(application:, service:), v)
        else
          v
        end
      end
    else
      v
    end
  end
end

#transform(ast) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/orchparty/transformations/variable.rb', line 10

def transform(ast)
  ast.applications.each do |_, application|
    application.services = application.services.each do |_, service|
      resolve(application, service, service)
    end
    application.volumes = application.volumes.each do |_, volume|
      resolve(application, volume, nil) if volume
    end
  end
  ast
end