Sha256: 35d5c330cfbfb09a01fe856655166425caf038eb83b5f9d2a18508a0d4ad32f9

Contents?: true

Size: 1.44 KB

Versions: 30

Compression:

Stored size: 1.44 KB

Contents

class KuberKit::Core::ContextHelper::ContextVars
  attr_reader :parent, :parent_name

  BuildArgUndefined = Class.new(KuberKit::Error)

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

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

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

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

    read(name)
  end

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

  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, variable_name, self)
    end

    value
  end

  private

    def format_arg(name)
      string = [@parent_name, name].compact.join(".")
      parent = @parent

      while parent do
        string = [parent.parent_name, string].compact.join(".")
        parent = parent.parent
      end

      string
    end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
kuber_kit-1.2.4 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.2.2 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.1.2 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.2.1 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.2.0 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.1.1 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.0.1 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-1.0.0 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.9 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.8 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.7 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.6 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.5 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.4 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.3 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.2 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.1 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.9.0 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.8.8 lib/kuber_kit/core/context_helper/context_vars.rb
kuber_kit-0.8.7 lib/kuber_kit/core/context_helper/context_vars.rb