Class: R10K::Settings::Collection
- Inherits:
-
Object
- Object
- R10K::Settings::Collection
- Includes:
- Helpers
- 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
-
#[](name) ⇒ Object
Access individual settings via a Hash-like interface.
-
#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.
Methods included from Helpers
Constructor Details
#initialize(name, settings) ⇒ Collection
Returns a new instance of Collection.
21 22 23 24 25 26 27 28 29 30 31 |
# 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 |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/r10k/settings/collection.rb', line 17 def name @name end |
Instance Method Details
#[](name) ⇒ Object
Access individual settings via a Hash-like interface.
99 100 101 |
# File 'lib/r10k/settings/collection.rb', line 99 def [](name) @settings[name] end |
#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.
48 49 50 51 52 53 54 55 56 57 58 |
# 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 |
#evaluate(newvalues) ⇒ Object
Assign new values, perform validation checks, and return the final values for this collection
35 36 37 38 39 |
# File 'lib/r10k/settings/collection.rb', line 35 def evaluate(newvalues) assign(newvalues) validate resolve end |
#resolve ⇒ Hash
Evaluate all settings and return a frozen hash of the final values.
88 89 90 91 92 93 94 95 96 |
# 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 ⇒ nil, Hash
Validate all settings and return validation errors
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 |