Class: KuberKit::Core::Templates::TemplateStore

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/core/templates/template_store.rb

Constant Summary collapse

NotFoundError =
Class.new(KuberKit::NotFoundError)
AlreadyAddedError =
Class.new(KuberKit::Error)

Instance Method Summary collapse

Instance Method Details

#add(template) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/kuber_kit/core/templates/template_store.rb', line 5

def add(template)
  @@templates ||= {}

  if !template.is_a?(KuberKit::Core::Templates::AbstractTemplate)
    raise ArgumentError.new("should be an instance of KuberKit::Core::Templates::AbstractTemplate, got: #{template.inspect}")
  end

  unless @@templates[template.name].nil?
    raise AlreadyAddedError, "template #{template.name} was already added"
  end

  @@templates[template.name] = template
end

#get(template_name) ⇒ Object



19
20
21
22
23
24
# File 'lib/kuber_kit/core/templates/template_store.rb', line 19

def get(template_name)
  template = get_from_configuration(template_name) || 
             get_global(template_name)

  template
end

#get_from_configuration(template_name) ⇒ Object



37
38
39
40
# File 'lib/kuber_kit/core/templates/template_store.rb', line 37

def get_from_configuration(template_name)
  templates = KuberKit.current_configuration.templates
  templates[template_name]
end

#get_global(template_name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/kuber_kit/core/templates/template_store.rb', line 26

def get_global(template_name)
  @@templates ||= {}
  template = @@templates[template_name]

  if template.nil?
    raise NotFoundError, "template '#{template_name}' not found"
  end
  
  template
end

#reset!Object



42
43
44
# File 'lib/kuber_kit/core/templates/template_store.rb', line 42

def reset!
  @@templates = {}
end