Class: KuberKit::Core::ContextHelper::ContextVars

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/core/context_helper/context_vars.rb

Constant Summary collapse

BuildArgUndefined =
Class.new(KuberKit::Error)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_vars, parent_name = nil) ⇒ ContextVars

Returns a new instance of ContextVars.



6
7
8
9
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 6

def initialize(context_vars, parent_name = nil)
  @context_vars = context_vars
  @parent_name  = parent_name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 32

def method_missing(name, *args)
  if args.size > 0
    raise ArgumentError.new("context args does not accept any arguments")
  end

  dig(name)
end

Instance Attribute Details

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 2

def parent
  @parent
end

#parent_name ⇒ Object (readonly)

Returns the value of attribute parent_name.



2
3
4
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 2

def parent_name
  @parent_name
end

Instance Method Details

#dig(*variable_names) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 17

def dig(*variable_names)
  result = self
  variable_names.each do |var|
    result = result.get_variable_value(var)
  end
  result
end

#get_variable_value(variable_name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 48

def get_variable_value(variable_name)
  value = @context_vars.fetch(variable_name) do
    raise(BuildArgUndefined, "build arg '#{format_arg(variable_name)}' is not defined, available args: #{@context_vars.inspect}")
  end

  if value.is_a?(Hash)
    return self.class.new(value, format_arg(variable_name))
  end

  value
end

#read(*variable_names, default: nil) ⇒ Object



11
12
13
14
15
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 11

def read(*variable_names, default: nil)
  dig(*variable_names)
rescue BuildArgUndefined
  return default
end

#to_h ⇒ Object



40
41
42
43
44
45
46
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 40

def to_h
  if @context_vars.is_a?(Hash)
    return @context_vars
  else
    return {value: @context_vars}
  end
end

#variable_defined?(*variable_names) ⇒ Boolean

Returns:



25
26
27
28
29
30
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 25

def variable_defined?(*variable_names)
  dig(*variable_names)
  return true
rescue BuildArgUndefined
  return false
end