Class: KubeManifest::Describe

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

Constant Summary collapse

SPEC_VAR =
'@@spec'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Describe

Returns a new instance of Describe.



6
7
8
# File 'lib/kube_manifest/describe.rb', line 6

def initialize(klass)
  @klass = klass
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kube_manifest/describe.rb', line 10

def method_missing(name, *args, &block)
  field_name = name.to_s.gsub(/^_/, '').to_sym
  field_type = args[0]
  is_spec = field_type.is_a?(Class) && field_type.ancestors.include?(::KubeManifest::Spec)
  is_array = args[1] == Array
  if is_spec && !field_type.class_variable_defined?(::KubeManifest::Describe::SPEC_VAR)
    field_type.class_variable_set(::KubeManifest::Describe::SPEC_VAR, {})
  end

  unless @klass.instance_methods.include? field_name
    method = if is_spec && is_array
               ::KubeManifest::SpecUtils.children_node(field_name, field_type)
             elsif is_spec && !is_array
               ::KubeManifest::SpecUtils.child_node(field_name, field_type)
             elsif !is_spec && is_array
               ::KubeManifest::SpecUtils.children_value(field_name)
             else
               ::KubeManifest::SpecUtils.child_value(field_name)
             end
    @klass.define_method(field_name, method)
    @klass.alias_method name, field_name
  end

  @klass.class_variable_get(::KubeManifest::Describe::SPEC_VAR)[field_name] = field_type
end

Class Method Details

.new_value(obj, klass, *args, **kwargs, &block) ⇒ Object



36
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
62
63
64
65
66
67
68
69
70
71
# File 'lib/kube_manifest/describe.rb', line 36

def self.new_value(obj, klass, *args, **kwargs, &block)
  if args.first.is_a? ::KubeManifest::Context
    returning = args.first.evaluate(overriding: obj._ctx)
    returning.instance_eval(&block) if block
    return returning
  elsif args.first.is_a? Array
    args = args.first
    returning = args.map do |arg|
      if arg.is_a? ::KubeManifest::Context
        arg.evaluate(overriding: obj._ctx)
      else
        arg
      end
    end
    return returning
  end

  returning = klass.new(ctx: obj._ctx, values: obj._values)
  returning.instance_eval(&block) if block
  kwargs.each_pair do |key, value|
    field_type = klass.class_variable_get(::KubeManifest::Describe::SPEC_VAR)[key]
    is_spec = field_type.is_a?(Class) && field_type.ancestors.include?(::KubeManifest::Spec)
    if is_spec
      if value.is_a? Hash
        child = ::KubeManifest::Describe.new_value(obj, field_type, *args, **value, &nil)
        returning.instance_variable_set("@#{key}", child)
      elsif value.is_a? Array
        child = value.map { |e| e.class == ::KubeManifest::Context ? e.evaluate(overriding: obj._ctx) : e }
        returning.instance_variable_set("@#{key}", child)
      end
    else
      returning.instance_variable_set("@#{key}", value)
    end
  end
  returning
end