Define a group of settings, which can be single definitions or nested collections.
@!attribute [r] name
@return [String] The name of this collection
@param name [Symbol] The name of the collection @param settings [Array] All settings in this collection
# File lib/r10k/settings/collection.rb, line 21 def initialize(name, settings) @name = name @settings = {} # iterate through settings and adopt them settings.each do |s| s.parent = self @settings[s.name] = s end end
Access individual settings via a Hash-like interface.
# File lib/r10k/settings/collection.rb, line 99 def [](name) @settings[name] end
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 48 def assign(newvalues) return if newvalues.nil? R10K::Util::SymbolizeKeys.symbolize_keys!(newvalues) @settings.each_pair do |name, setting| if newvalues.key?(name) setting.assign(newvalues[name]) end end end
Assign new values, perform validation checks, and return the final values for this collection
# File lib/r10k/settings/collection.rb, line 35 def evaluate(newvalues) assign(newvalues) validate resolve end
Evaluate all settings and return a frozen hash of the final values. @return [Hash]
# File lib/r10k/settings/collection.rb, line 88 def resolve rv = {} @settings.each_pair do |name, setting| rv[name] = setting.resolve end rv.freeze end
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 64 def validate errors = {} @settings.each_pair do |name, setting| begin setting.validate rescue => error errors[name] = error end end if !errors.empty? if @name msg = _("Validation failed for '%{name}' settings group") % {name: @name} else msg = _("Validation failed for settings group") end raise ValidationError.new(msg, :errors => errors) end end