Sha256: b547d55a668e756b4570aa35c8462bfca2141d1c94df812838e67e304773e526

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Kubec
  class Kubernetes
    # :nodoc:
    class ConfigMap < Hash
      attr_reader :name, :files

      def initialize(name, &block)
        @name = name.to_sym
        @files = {}

        prepare
        instance_eval(&block)
      end

      # TODO: Refactor this feature
      def fetch(*args)
        Environment.instance.fetch(*args)
      end

      def set(key, value)
        self[:data][key] = value
      end

      # TODO: Refactor
      def file(path)
        key = path.split('/').last
        @files[key] = path
        path = path_with_stage(path) if stage_config_exist?(path)
        set key, File.read(path)
      end

      private

      def prepare
        self[:apiVersion] = 'v1'
        self[:metadata] = Metadata.new(@name)
        self[:kind] = 'ConfigMap'
        self[:data] = {}
      end

      def stage_config_exist?(path)
        File.exist?(path_with_stage(path))
      end

      def path_with_stage(path)
        path.split('.').tap do |ary|
          ext = ary.pop
          ary.push(fetch(:stage, :staging))
          ary.push(ext)
        end.join('.')
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kubec-0.4.1 lib/kubec/kubernetes/config_map.rb
kubec-0.4.0 lib/kubec/kubernetes/config_map.rb
kubec-0.3.6 lib/kubec/kubernetes/config_map.rb
kubec-0.3.5 lib/kubec/kubernetes/config_map.rb