Class: R10K::Settings::List

Inherits:
Object
  • Object
show all
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

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level

Methods included from Helpers

included

Constructor Details

#initialize(name, item_proc, opts = {}) ⇒ List

Returns a new instance of List.

Parameters:

  • name (Symbol)

    The name of the setting for this definition.

  • item_proc (#call)

    An object whose #call method will return a new instance of another R10K::Settings class to hold each item added to this list.

  • opts (Hash) (defaults to: {})

    Additional options for this definition to control validation, normalization, and the like.



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

#nameObject (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.

Parameters:

  • items (Array)

    List of items to add to this list.



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

#resolveArray

Evaluate all items in the list and return a frozen array of the final values.

Returns:

  • (Array)


72
73
74
# File 'lib/r10k/settings/list.rb', line 72

def resolve
  @items.collect { |item| item.resolve }.freeze
end

#validatenil, Hash

Validate all items in the list and return validation errors

Returns:

  • (nil, Hash)

    If all validation passed nil will be returned; if validation failed then a hash of those errors will be returned.



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