Class: R10K::Settings::List
- Inherits:
-
Object
- Object
- R10K::Settings::List
- Includes:
- Helpers, Util::Setopts
- Defined in:
- lib/r10k/settings/list.rb
Overview
A container for an arbitrarily long list of other settings.
Defined Under Namespace
Classes: ValidationError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#assign(items) ⇒ Object
Takes an array of key/value pairs and assigns each into a new instance created by invoking @item_proc.
-
#initialize(name, item_proc, opts = {}) ⇒ List
constructor
A new instance of List.
-
#resolve ⇒ Array
Evaluate all items in the list and return a frozen array of the final values.
-
#validate ⇒ nil, Hash
Validate all items in the list and return validation errors.
Methods included from Helpers
Constructor Details
#initialize(name, item_proc, opts = {}) ⇒ List
Returns a new instance of List.
27 28 29 30 31 32 33 |
# File 'lib/r10k/settings/list.rb', line 27 def initialize(name, item_proc, opts = {}) @name = name @item_proc = item_proc @items = [] setopts(opts, allowed_initialize_opts) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/r10k/settings/list.rb', line 16 def name @name end |
Instance Method Details
#assign(items) ⇒ Object
Takes an array of key/value pairs and assigns each into a new instance created by invoking @item_proc.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/r10k/settings/list.rb', line 39 def assign(items) return if items.nil? items.each do |values| new_item = @item_proc.call new_item.parent = self new_item.assign(values) @items << new_item end end |
#resolve ⇒ Array
Evaluate all items in the list and return a frozen array of the final values.
72 73 74 |
# File 'lib/r10k/settings/list.rb', line 72 def resolve @items.collect { |item| item.resolve }.freeze end |
#validate ⇒ nil, Hash
Validate all items in the list and return validation errors
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/r10k/settings/list.rb', line 54 def validate errors = {} @items.each_with_index do |item, idx| begin item.validate rescue => error errors[idx+1] = error end end if !errors.empty? raise ValidationError.new(_("Validation failed for '%{name}' settings list") % {name: @name}, :errors => errors) end end |