Class: R10K::Settings::Collection
- Inherits:
-
Object
- Object
- R10K::Settings::Collection
- Defined in:
- lib/r10k/settings/collection.rb
Overview
Define a group of settings, which can be single definitions or nested collections.
Defined Under Namespace
Classes: ValidationError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#assign(newvalues) ⇒ void
Assign a hash of values to the settings in this collection.
-
#evaluate(newvalues) ⇒ Object
Assign new values, perform validation checks, and return the final values for this collection.
-
#initialize(name, settings) ⇒ Collection
constructor
A new instance of Collection.
-
#resolve ⇒ Hash
Evaluate all settings and return a frozen hash of the final values.
-
#validate ⇒ nil, Hash
Validate all settings and return validation errors.
Constructor Details
#initialize(name, settings) ⇒ Collection
Returns a new instance of Collection.
19 20 21 22 |
# File 'lib/r10k/settings/collection.rb', line 19 def initialize(name, settings) @name = name @settings = Hash[settings.map { |s| [s.name, s] }] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/r10k/settings/collection.rb', line 15 def name @name end |
Instance Method Details
#assign(newvalues) ⇒ void
This method returns an undefined value.
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.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/r10k/settings/collection.rb', line 39 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 |
#evaluate(newvalues) ⇒ Object
Assign new values, perform validation checks, and return the final values for this collection
26 27 28 29 30 |
# File 'lib/r10k/settings/collection.rb', line 26 def evaluate(newvalues) assign(newvalues) validate resolve end |
#resolve ⇒ Hash
Evaluate all settings and return a frozen hash of the final values.
72 73 74 75 76 77 78 |
# File 'lib/r10k/settings/collection.rb', line 72 def resolve rv = {} @settings.each_pair do |name, setting| rv[name] = setting.resolve end rv.freeze end |
#validate ⇒ nil, Hash
Validate all settings and return validation errors
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/r10k/settings/collection.rb', line 54 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 |