Class: KubeDSL::Builder

Inherits:
Object
  • Object
show all
Includes:
StringHelpers
Defined in:
lib/kube-dsl/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringHelpers

#capitalize, #underscore

Constructor Details

#initialize(schema_dir:, output_dir:, inflector:) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
14
# File 'lib/kube-dsl/builder.rb', line 9

def initialize(schema_dir:, output_dir:, inflector:)
  @schema_dir = schema_dir
  @output_dir = output_dir
  @inflector = inflector
  @resolvers ||= {}
end

Instance Attribute Details

#inflectorObject (readonly)

Returns the value of attribute inflector.



7
8
9
# File 'lib/kube-dsl/builder.rb', line 7

def inflector
  @inflector
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/kube-dsl/builder.rb', line 7

def namespace
  @namespace
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



7
8
9
# File 'lib/kube-dsl/builder.rb', line 7

def output_dir
  @output_dir
end

#resolversObject (readonly)

Returns the value of attribute resolvers.



7
8
9
# File 'lib/kube-dsl/builder.rb', line 7

def resolvers
  @resolvers
end

#schema_dirObject (readonly)

Returns the value of attribute schema_dir.



7
8
9
# File 'lib/kube-dsl/builder.rb', line 7

def schema_dir
  @schema_dir
end

Instance Method Details

#each_autoload_file(&block) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/kube-dsl/builder.rb', line 69

def each_autoload_file(&block)
  return to_enum(__method__) unless block

  start = output_dir.split(File::SEPARATOR).first

  each_autoload_file_helper(
    autoload_map[start], [start], &block
  )
end

#each_resourceObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kube-dsl/builder.rb', line 22

def each_resource
  return to_enum(__method__) unless block_given?

  resources.each do |res|
    # "External" resources are ones that live outside the current
    # schema, i.e. k8s resources like ObjectMeta that other
    # k8s-compatible schemas depend on.
    #
    # Resources can be "empty" if they contain no properties. This
    # usually happens for resources that are really just aliases
    # for basic types like integer and string. The k8s' Duration
    # object is a good example. It's just an alias for string.
    yield res if !res.external? && !res.empty?
  end
end

#entrypoint(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kube-dsl/builder.rb', line 38

def entrypoint(&block)
  ''.tap do |ruby_code|
    ruby_code << "module #{namespace[0..-2].join('::')}::Entrypoint\n"

    each_resource do |resource|
      ns = resource.ref.ruby_namespace.join('::')
      next if block && !block.call(resource, ns)

      ruby_code << "  def #{underscore(resource.ref.kind)}(&block)\n"
      ruby_code << "    ::#{ns}::#{resource.ref.kind}.new(&block)\n"
      ruby_code << "  end\n\n"
    end

    ruby_code.strip!
    ruby_code << "\nend\n"
  end
end

#entrypoint_pathObject



65
66
67
# File 'lib/kube-dsl/builder.rb', line 65

def entrypoint_path
  File.join(File.dirname(output_dir), 'entrypoint.rb')
end

#parse_ref(ref_str) ⇒ Object



91
92
93
# File 'lib/kube-dsl/builder.rb', line 91

def parse_ref(ref_str)
  Ref.new(ref_str, namespace, output_dir, inflector, schema_dir)
end

#register_resolver(*prefixes, &resolver) ⇒ Object



16
17
18
19
20
# File 'lib/kube-dsl/builder.rb', line 16

def register_resolver(*prefixes, &resolver)
  prefixes.each do |prefix|
    @resolvers[prefix] = resolver
  end
end

#resource_from_ref(ref) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kube-dsl/builder.rb', line 79

def resource_from_ref(ref)
  if res = resource_cache[ref.str]
    return res
  end

  res = resource_cache[ref.str] = ref.meta

  add_doc_to_resource(res, ref.document)

  res
end