class R10K::Settings::Collection

Define a group of settings, which can be single definitions or nested collections.

Attributes

name[R]

@!attribute [r] name

@return [String] The name of this collection

Public Class Methods

new(name, settings) click to toggle source

@param name [Symbol] The name of the collection @param settings [Array] All settings in this collection

# File lib/r10k/settings/collection.rb, line 19
def initialize(name, settings)
  @name = name
  @settings = Hash[settings.map { |s| [s.name, s] }]
end

Public Instance Methods

assign(newvalues) click to toggle source

Assign a hash of values to the settings in this collection.

If the passed hash contains any invalid settings values, the names of those settings are stored for use in the {#validate} method.

@param newvalues [Hash] @return [void]

# File lib/r10k/settings/collection.rb, line 39
def assign(newvalues)
  return if newvalues.nil?

  # TODO: this results in inconsistency for hashes inside arrays.
  R10K::Util::SymbolizeKeys.symbolize_keys!(newvalues)
  @settings.each_pair do |name, setting|
    if newvalues.key?(name)
      setting.assign(newvalues[name])
    end
  end
end
evaluate(newvalues) click to toggle source

Assign new values, perform validation checks, and return the final values for this collection

# File lib/r10k/settings/collection.rb, line 26
def evaluate(newvalues)
  assign(newvalues)
  validate
  resolve
end
resolve() click to toggle source

Evaluate all settings and return a frozen hash of the final values. @return [Hash]

# File lib/r10k/settings/collection.rb, line 73
def resolve
  rv = {}
  @settings.each_pair do |name, setting|
    rv[name] = setting.resolve
  end
  rv.freeze
end
validate() click to toggle source

Validate all settings and return validation errors

@return [nil, Hash] If all validation passed nil will be returned; if

validation failed then a hash of those errors will be returned.
# File lib/r10k/settings/collection.rb, line 55
def validate
  errors = {}

  @settings.each_pair do |name, setting|
    begin
      setting.validate
    rescue => error
      errors[name] = error
    end
  end

  if !errors.empty?
    raise ValidationError.new("Validation failed for #{@name} settings group", :errors => errors)
  end
end