Class: R10K::Settings::Definition

Inherits:
Object
  • Object
show all
Includes:
Helpers, Util::Setopts
Defined in:
lib/r10k/settings/definition.rb

Overview

Define a single setting and additional attributes like descriptions, default values, and validation.

Direct Known Subclasses

EnumDefinition, URIDefinition

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, opts = {}) ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • name (Symbol)

    The name of the setting for this definition.

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

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

Options Hash (opts):

  • :default (Proc, Object)

    An optional proc or object for this setting. If no value has been set and the default is a Proc then it will be called and the result will be returned, otherwise if the value is not set the default value itself is returned.



43
44
45
46
# File 'lib/r10k/settings/definition.rb', line 43

def initialize(name, opts = {})
  @name = name
  setopts(opts, allowed_initialize_opts)
end

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



27
28
29
# File 'lib/r10k/settings/definition.rb', line 27

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/r10k/settings/definition.rb', line 18

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



23
24
25
# File 'lib/r10k/settings/definition.rb', line 23

def value
  @value
end

Instance Method Details

#assign(newvalue) ⇒ void

This method returns an undefined value.

Store an explicit value for this definition

If a :normalize hook has been given then it will be called with the new value and the returned value will be stored.

Parameters:

  • newvalue (Object)

    The value to store for this setting



63
64
65
66
67
68
69
# File 'lib/r10k/settings/definition.rb', line 63

def assign(newvalue)
  if @normalize
    @value = @normalize.call(newvalue)
  else
    @value = newvalue
  end
end

#evaluate(newvalue) ⇒ Object

Assign new values, perform validation checks, and return the final values for this collection



50
51
52
53
54
# File 'lib/r10k/settings/definition.rb', line 50

def evaluate(newvalue)
  assign(newvalue)
  validate
  resolve
end

#resolveObject

Compute the final value of this setting. If a value has not been assigned the default value will be used.

Returns:

  • (Object)

    The final value of this definition.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/r10k/settings/definition.rb', line 90

def resolve
  if !@value.nil?
    @value
  elsif !@default.nil?
    if @default == :inherit
      # walk all the way up to root, starting with grandparent
      ancestor = parent

      while ancestor = ancestor.parent
        return ancestor[@name].resolve if ancestor.respond_to?(:[]) && ancestor[@name]
      end
    elsif @default.is_a?(Proc)
      @default.call
    else
      @default
    end
  end
end

#validatenil

Call any validation hooks for this definition.

The :validate hook will be called if the hook has been set and an explicit value has been assigned to this definition. Validation failures should be indicated by the :validate hook raising an exception.

Returns:

  • (nil)

Raises:

  • (Exception)

    An exception class indicating that validation failed.



79
80
81
82
83
84
# File 'lib/r10k/settings/definition.rb', line 79

def validate
  if @value && @validate
    @validate.call(@value)
  end
  nil
end