Class: Kubes::Compiler::Strategy::Dispatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/kubes/compiler/strategy/dispatcher.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Util::Pretty

#pretty_path

Methods included from Logging

#logger

Methods included from Util::SaveFile

#save_file

Methods included from Util::Normalize

#extract_type, #normalize_kind

Methods included from Layering

#add_exts, #post_layers, #pre_layers

Constructor Details

This class inherits a constructor from Kubes::Compiler::Strategy::Base

Instance Method Details

#block_form?(path) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 52

def block_form?(path)
  type = extract_type(path)
  type.pluralize == type
end

#dispatchObject



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 3

def dispatch
  show_layers if Kubes.config.layering.show
  result = render(@path) # main
  results = [result].flatten # ensure array
  data = results.map! do |main|
    hash = Kubes.deep_merge!(pre_layer, main)
    Kubes.deep_merge!(hash, post_layer)
  end
  logger.info "Compiled .kubes/output/#{@save_file}" if Kubes.config.layering.show
  Result.new(@save_file, data)
end

#dsl_class(path) ⇒ Object

Must be defined here in case coming from Kubes::Compiler::Strategy::Erb#render



36
37
38
39
40
41
42
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 36

def dsl_class(path)
  if block_form?(path)
    Kubes::Compiler::Dsl::Core::Blocks
  else
    syntax_class
  end
end

#merge_layers(layers) ⇒ Object



65
66
67
68
69
70
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 65

def merge_layers(layers)
  layers.inject({}) do |hash, layer|
    data = render(layer)
    hash.deep_merge!(data)
  end
end

#post_layerObject



61
62
63
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 61

def post_layer
  merge_layers(post_layers)
end

#pre_layerObject



57
58
59
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 57

def pre_layer
  merge_layers(pre_layers)
end

#render(path) ⇒ Object

Render via to Erb or one of the DSL syntax classes or Core/Blocks class



25
26
27
28
29
30
31
32
33
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 25

def render(path)
  if path.include?('.rb')
    klass = dsl_class(path) # IE: Kubes::Compiler::Dsl::Syntax::Deployment or Kubes::Compiler::Dsl::Core::Blocks
    klass.new(@options.merge(path: path, data: @data)).run
  else
    logger.info "Rendering #{pretty_path(path)}" if ENV['KUBES_LAYERING_SHOW_RENDERING']
    Erb.new(@options.merge(data: @data)).render_result(path)
  end
end

#show_layersObject



15
16
17
18
19
20
21
22
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 15

def show_layers
  logger.info "Compiling #{pretty_path(@path)}"
  logger.info "    Resource layers:"
  all_layers = pre_layers + [@path] + post_layers
  all_layers.each do |layer|
    logger.info "    #{pretty_path(layer)}"
  end
end

#syntax_classObject



44
45
46
47
48
49
50
# File 'lib/kubes/compiler/strategy/dispatcher.rb', line 44

def syntax_class
  klass_name = normalize_kind(@save_file) # IE: @save_file: web/service.yaml
  "Kubes::Compiler::Dsl::Syntax::#{klass_name}".constantize
rescue NameError
  logger.debug "Using default resource for: #{klass_name}"
  Kubes::Compiler::Dsl::Syntax::Resource # default
end