Class: R10K::Settings::Definition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



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

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

Instance Attribute Details

#descObject (readonly)

Returns the value of attribute desc.



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

def desc
  @desc
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



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

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



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

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



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

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.



89
90
91
92
93
94
95
# File 'lib/r10k/settings/definition.rb', line 89

def resolve
  if !@value.nil?
    @value
  elsif @default
    @default.is_a?(Proc) ? @default.call : @default
  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.



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

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