Sha256: 0b338ed8573239581410895fa3665c897eb9371ab3419757c661dddaf479f7d6

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

class Kubes::Compiler::Strategy
  class Dispatcher < Base
    def dispatch
      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
      Result.new(@save_file, data)
    end

    # Render via to Erb or one of the DSL syntax classes or Core/Blocks class
    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
        Erb.new(@options.merge(data: @data)).render_result(path)
      end
    end

    # Must be defined here in case coming from Kubes::Compiler::Strategy::Erb#render
    def dsl_class(path)
      if block_form?(path)
        Kubes::Compiler::Dsl::Core::Blocks
      else
        syntax_class
      end
    end

    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

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

    def pre_layer
      merge_layers(pre_layers)
    end

    def post_layer
      merge_layers(post_layers)
    end

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
kubes-0.8.9 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.8 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.7 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.6 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.5 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.4 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.3 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.2 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.1 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.8.0 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.10 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.9 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.8 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.7 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.6 lib/kubes/compiler/strategy/dispatcher.rb
kubes-0.7.5 lib/kubes/compiler/strategy/dispatcher.rb