class Kybus::Configuration::Loaders::Arg

This class allows to load configurations from ARGV It requires that all the vars are named with a common prefix It uses '__' as a delimiter to allow nested configurations

Examples

--config_env_value=3 => { 'env_value' => '3' }
--config_env_value 3 => { 'env_value' => '3' }
--config_env_obj__value 3 => { "env_obj" => { 'value' => '3' } }
--config_flag --config_value 3 => { 'flag' => 'true', value => '3' }

Public Class Methods

new(env_prefix, manager, array = ARGV) click to toggle source
# File lib/kybus/configs/loaders/arg.rb, line 22
def initialize(env_prefix, manager, array = ARGV)
  @env_prefix = env_prefix.downcase
  @manager = manager
  @array = array
end

Public Instance Methods

extract_value(string, idx) click to toggle source

Parses a string as described above

# File lib/kybus/configs/loaders/arg.rb, line 44
def extract_value(string, idx)
  if string.include?('=')
    string.split('=')[1]
  elsif @array.size == idx
    'true'
  elsif @array[idx].start_with?('--')
    'true'
  else
    @array[idx]
  end
end
load!() click to toggle source

Parses configurations from array and returns the value as a hash

# File lib/kybus/configs/loaders/arg.rb, line 29
def load!
  configs = {}
  @array.each_with_index do |obj, idx|
    next unless obj.start_with?('--' + @env_prefix)

    value = extract_value(obj, idx + 1)
    key = obj.split('=').first
             .sub(/^--#{@env_prefix}_?/, '')
             .downcase.split('__')
    recursive_set(configs, key, split_env_string(value))
  end
  configs
end