Class: KubeManifest::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/kube_manifest/runner.rb

Constant Summary collapse

@@MODULE_CACHE =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, values: {}, cwd: []) ⇒ Runner

Returns a new instance of Runner.



4
5
6
# File 'lib/kube_manifest/runner.rb', line 4

def initialize(code, values: {}, cwd: [])
  @code, @values, @cwd = code, values, cwd
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/kube_manifest/runner.rb', line 29

def method_missing(name, *args, **kwargs, &block)
  klass_name = name.to_s
               .sub(/^[a-z\d]*/) { |match| match.capitalize }
               .gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
  klass = ::KubeManifest::Spec.const_get(klass_name)
  ::KubeManifest::Context.new(klass, **kwargs, &block)
end

Class Method Details

.load_mixin!(mixin) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kube_manifest/runner.rb', line 37

def self.load_mixin!(mixin)
  if mixin.is_a? Module
    return KubeManifest::Spec.include(mixin)
  end

  return unless mixin.is_a?(String)
  return unless File.exists? mixin

  mod = @@MODULE_CACHE[mixin] || Module.new do
    eval(File.open(mixin).read)

    # TODO merge with above
    def method_missing(name, *args, **kwargs, &block)
      klass_name = name.to_s
                       .sub(/^[a-z\d]*/) { |match| match.capitalize }
                       .gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
      klass = ::KubeManifest::Spec.const_get(klass_name)
      ::KubeManifest::Context.new(klass, **kwargs, &block)
    end
  end

  @@MODULE_CACHE[mixin] = mod

  KubeManifest::Spec.include(mod)
end

.unload_mixin!(mixin) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kube_manifest/runner.rb', line 63

def self.unload_mixin!(mixin)
  filename = if Pathname.new(mixin).absolute?
               mixin
             else
               Pathname.new(File.join(Dir.pwd, mixin)).expand_path.to_s
             end

  return unless @@MODULE_CACHE.include? filename
  @@MODULE_CACHE[filename].instance_methods.each do |m|
    KubeManifest::Spec.undef_method(m)
  end
end

Instance Method Details

#_valuesObject



25
26
27
# File 'lib/kube_manifest/runner.rb', line 25

def _values
  @values
end

#ctxObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kube_manifest/runner.rb', line 8

def ctx
  result = instance_eval(@code)
  if result.is_a? Array
    result.select{ |c| c }.map do |c|
      c.cwd = @cwd
      c.values = @values
      c
    end
  elsif result.nil?
    nil
  else
    result.cwd = @cwd
    result.values = @values
    result
  end
end