Sha256: 557f92559849d608603fe43a7e043adce7309a6587502f08a7961f50f110bbdf

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

class Kubes::Compiler::Strategy
  class Base
    include Kubes::Compiler::Layering
    include Kubes::Compiler::Util::Normalize
    include Kubes::Compiler::Util::SaveFile
    include Kubes::Logging

    def initialize(options={})
      @options = options
      @path = options[:path]
      @save_file = save_file(@path)
      @data = @options[:data] || {}
    end

    def run
      render_files(pre_layers)
      render(@path) # main resource definition
      render_files(post_layers)

      Result.new(@save_file, @data)
    end

    def render_files(paths)
      paths.each do |path|
        next unless File.exist?(path) # layers may not exist
        render(path)
      end
    end

    # render and merge
    def render(path)
      result = render_strategy(path)
      if result.is_a?(Kubes::Compiler::Dsl::Core::Blocks)
        result = result.results
      end
      @data.deeper_merge!(result)
    end

    # Delegate to Dsl or Erb strategy again and pass @data down to allow rendering of a mix of yaml and rb files.
    def render_strategy(path)
      if path.include?('.rb')
        dsl_class.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_strategy
    def dsl_class
      if block_form?
        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?
      type = extract_type(@save_file)
      type.pluralize == type
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kubes-0.6.8 lib/kubes/compiler/strategy/base.rb
kubes-0.6.7 lib/kubes/compiler/strategy/base.rb
kubes-0.6.6 lib/kubes/compiler/strategy/base.rb
kubes-0.6.5 lib/kubes/compiler/strategy/base.rb
kubes-0.6.4 lib/kubes/compiler/strategy/base.rb
kubes-0.6.3 lib/kubes/compiler/strategy/base.rb
kubes-0.6.2 lib/kubes/compiler/strategy/base.rb
kubes-0.6.1 lib/kubes/compiler/strategy/base.rb
kubes-0.6.0 lib/kubes/compiler/strategy/base.rb