Class: R10K::Settings::Definition
- Inherits:
-
Object
- Object
- R10K::Settings::Definition
- 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
Constant Summary
Constants included from Logging
Logging::LOG_LEVELS, Logging::SYSLOG_LEVELS_MAP
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#assign(newvalue) ⇒ void
Store an explicit value for this definition.
-
#evaluate(newvalue) ⇒ Object
Assign new values, perform validation checks, and return the final values for this collection.
-
#initialize(name, opts = {}) ⇒ Definition
constructor
A new instance of Definition.
-
#resolve ⇒ Object
Compute the final value of this setting.
-
#validate ⇒ nil
Call any validation hooks for this definition.
Methods included from Logging
add_outputters, debug_formatter, default_formatter, default_outputter, #logger, #logger_name, parse_level
Methods included from Helpers
Constructor Details
#initialize(name, opts = {}) ⇒ Definition
Returns a new instance of Definition.
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
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
27 28 29 |
# File 'lib/r10k/settings/definition.rb', line 27 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/r10k/settings/definition.rb', line 18 def name @name end |
#value ⇒ Object (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.
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 |
#resolve ⇒ Object
Compute the final value of this setting. If a value has not been assigned the default value will be used.
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 |
#validate ⇒ nil
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.
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 |