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.



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

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



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

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

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#parent_nameObject (readonly)

Returns the value of attribute parent_name.



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

def parent_name
  @parent_name
end

Instance Method Details

#dig(*variable_names) ⇒ Object



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

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



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 64

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

#keysObject



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

def keys
  @context_vars.keys
end

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



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

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

#to_hObject



45
46
47
48
49
50
51
52
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 45

def to_h
  values = keys.map do |key|
    value = get_variable_value(key)
    hash_value = value.respond_to?(:to_h) ? value.to_h : value
    [key, hash_value]
  end
  Hash[values]
end

#to_structObject



54
55
56
57
58
59
60
61
62
# File 'lib/kuber_kit/core/context_helper/context_vars.rb', line 54

def to_struct
  values = keys.map do |key|
    value = get_variable_value(key)
    hash_value = value.respond_to?(:to_struct) ? value.to_struct : value
    [key, hash_value]
  end
  hash = Hash[values]
  OpenStruct.new(hash)
end

#variable_defined?(*variable_names) ⇒ Boolean

Returns:

  • (Boolean)


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

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