Class: ConfigOMat::LoadedAppconfigProfile

Inherits:
ConfigItem
  • Object
show all
Defined in:
lib/config_o_mat/shared/types.rb

Constant Summary collapse

PARSERS =
{
  'text/plain' => proc { |str| str },
  'application/json' => proc { |str| JSON.parse(str, symbolize_names: true) },
  'application/x-yaml' => proc { |str| YAML.safe_load(str, symbolize_names: true) }
}.freeze

Instance Attribute Summary collapse

Attributes inherited from ConfigItem

#errors

Instance Method Summary collapse

Methods inherited from ConfigItem

#==, #error, #errors?, #validate!

Constructor Details

#initialize(name, version, contents, content_type, fallback = false) ⇒ LoadedAppconfigProfile

Returns a new instance of LoadedAppconfigProfile.



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/config_o_mat/shared/types.rb', line 297

def initialize(name, version, contents, content_type, fallback = false)
  @name = name
  @version = version
  @secret_defs = {}
  @fallback = fallback
  @chaos_config = false

  parser = PARSERS[content_type]

  if parser
    begin
      @contents = parser.call(contents)
      if @contents.kind_of?(Hash)
        parse_secrets
        @chaos_config = @contents.fetch(:"aws:chaos_config", false)
        @contents.default_proc = proc do |hash, key|
          raise KeyError.new("No key #{key.inspect} in profile #{name}", key: key, receiver: hash)
        end
      end
    rescue StandardError => e
      error :contents, e
    end
  else
    error :content_type, "must be one of #{PARSERS.keys}"
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



289
290
291
# File 'lib/config_o_mat/shared/types.rb', line 289

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



289
290
291
# File 'lib/config_o_mat/shared/types.rb', line 289

def name
  @name
end

#secret_defsObject (readonly)

Returns the value of attribute secret_defs.



289
290
291
# File 'lib/config_o_mat/shared/types.rb', line 289

def secret_defs
  @secret_defs
end

#versionObject (readonly)

Returns the value of attribute version.



289
290
291
# File 'lib/config_o_mat/shared/types.rb', line 289

def version
  @version
end

Instance Method Details

#chaos_config?Boolean

Returns:

  • (Boolean)


343
344
345
# File 'lib/config_o_mat/shared/types.rb', line 343

def chaos_config?
  @chaos_config
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


347
348
349
350
351
352
353
354
355
356
357
# File 'lib/config_o_mat/shared/types.rb', line 347

def eql?(other)
  return false if !super(other)
  if other.version != version ||
     other.contents != contents ||
     other.name != name ||
     other.fallback? != fallback? ||
     other.chaos_config? != chaos_config?
    return false
  end
  true
end

#fallback?Boolean

Returns:

  • (Boolean)


339
340
341
# File 'lib/config_o_mat/shared/types.rb', line 339

def fallback?
  @fallback
end

#hashObject



331
332
333
# File 'lib/config_o_mat/shared/types.rb', line 331

def hash
  @name.hash ^ @version.hash ^ @contents.hash ^ @fallback.hash ^ @chaos_config.hash
end

#to_hObject



335
336
337
# File 'lib/config_o_mat/shared/types.rb', line 335

def to_h
  @contents
end

#validateObject



324
325
326
327
328
329
# File 'lib/config_o_mat/shared/types.rb', line 324

def validate
  error :name, PRESENCE_ERROR_MSG if @name.nil? || @name.empty?
  error :name, 'must be a Symbol' unless @name.is_a?(Symbol)
  error :version, PRESENCE_ERROR_MSG if @version.nil? || @version.empty?
  error :contents, PRESENCE_ERROR_MSG if @contents.nil? || @contents.empty?
end