Sha256: e0a92acfcdf95fdb49783ff1a02c270621383b2bc2a6e15d2596568182705fec

Contents?: true

Size: 1.76 KB

Versions: 16

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require_relative '../../application'

# Class Case::Config
# * get
# * set
# * unset
# * missing_method
class Case
  # This class manage configuration for only one case
  class Config
    attr_reader :ialias, :global, :local, :running

    def initialize(args)
      @ialias  = args[:alias] || Application.instance.ialias.clone
      @global  = args[:global] || Application.instance.global.clone
      @local   = args[:local]  || {}
      @running = {}

      # Set defaults values
      @local[:tt_skip] = @local[:tt_skip] || false
    end

    # Read param Option from [running, config or global] Hash data
    def get(option, level = 0)
      return 'NODATA' if level > 3

      return @running[option] if @running[option]

      return @local[option] if @local[option]

      return @global[option] if @global[option]

      search_alias option, level + 1
    end

    def set(key, value)
      @running[key] = value
    end

    def unset(key)
      @running.delete(key)
    end

    private

    def search_alias(key, level)
      if @ialias[key].class == Array
        return search_array_alias(@ialias[key], level)
      elsif [Integer, String, Symbol].include? @ialias[key].class
        return get(@ialias[key])
      end
      
      words = key.to_s.split('_')
      return 'NODATA' if words.size < 2

      return 'NODATA' unless %w[ip hostname username password].include? words[1]

      key2 = @ialias[words[0].to_sym]
      return 'NODATA' unless key2

      get("#{key2}_#{words[1]}".to_sym, level)
    end

    def search_array_alias(keys, level)
      values = []
      keys.each do |k|
        if k.class == Symbol
          values << get(k, level + 1)
          next
        end
        values << k
      end
      values.join('')
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
teuton-2.3.6 lib/teuton/case_manager/case/config.rb
teuton-2.3.5 lib/teuton/case_manager/case/config.rb
teuton-2.3.4 lib/teuton/case_manager/case/config.rb
teuton-2.3.3 lib/teuton/case_manager/case/config.rb
teuton-2.3.2 lib/teuton/case_manager/case/config.rb
teuton-2.3.1 lib/teuton/case_manager/case/config.rb
teuton-2.2.2 lib/teuton/case_manager/case/config.rb
teuton-2.1.11 lib/teuton/case_manager/case/config.rb
teuton-2.1.10 lib/teuton/case_manager/case/config.rb
teuton-2.1.9 lib/teuton/case_manager/case/config.rb
teuton-2.1.8 lib/teuton/case_manager/case/config.rb
teuton-2.1.8dev1 lib/teuton/case_manager/case/config.rb
teuton-2.1.7 lib/teuton/case_manager/case/config.rb
teuton-2.1.6 lib/teuton/case_manager/case/config.rb
teuton-2.1.5dev lib/teuton/case_manager/case/config.rb
teuton-2.1.4 lib/teuton/case_manager/case/config.rb