class Nucleon::Config::Options
Contextualized option collection¶ ↑
The Nucleon::Config::Options
class defines a container for contextualized access of grouped properties.
This basically does three things:
-
Group properties by contextual identifiers and store in a centralized location
-
Access all defined properties for one or more contextual identifiers
-
Clear contextual properties
Right now, largely for historical reasons, this class is structured as a global interface and collection for grouping all of the defined options. It was originally contained within Nucleon::Config
as part of the global configuration interface. In the future, this class will be refactored to support multiple collections of contextualized properties.
For usage:
-
See core configuration object
Nucleon::Config::init
-
See configuration mixin
Nucleon::Mixin::ConfigOptions
Public Class Methods
Clear all properties for specified contexts.
Contexts are entirely removed, even the name itself. If nil is given (default) then all data is removed and options are reinitialized.
-
Parameters
- nil, Array<String, Symbol>, String, Symbol
-
contexts Context names to remove
-
Returns
- Void
-
This method does not currently have a return value
-
Errors
# File lib/core/config/options.rb 186 def self.clear(contexts = nil) 187 if contexts.nil? 188 @@options = {} 189 else 190 unless contexts.is_a?(Array) 191 contexts = [ contexts ] 192 end 193 contexts.each do |name| 194 @@options.delete(name.to_sym) 195 end 196 end 197 end
Return an array of context names based on given contexts and an optional hierarchy path.
This method mainly exists to allow us to create cascading context groups for the properties based on a hierarchical list. We use it to create contextual property lookups for configuring Puppet in the corl gem.
For example:
contexts = Nucleon::Config::Options.contexts([ :parameter, :var_name ], :module) contexts = [ 'all', 'parameter', 'var_name', 'module_parameter', 'module_var_name' ]
-
Parameters
- Array<String, Symbol>, String, Symbol
-
contexts Context names to include in list
- Array<String, Symbol>, String, Symbol
-
hierarchy Hierarchy of prefixes to apply to given contexts
-
Returns
- Array<String>
-
Generated array of ordered context names
-
Errors
See also:
# File lib/core/config/options.rb 68 def self.contexts(contexts = [], hierarchy = []) 69 contexts = [ 'all', contexts ].flatten 70 results = contexts 71 72 unless hierarchy.is_a?(Array) 73 hierarchy = ( ! Util::Data.empty?(hierarchy) ? [ hierarchy ].flatten : [] ) 74 end 75 76 hierarchy.each do |group| 77 group_contexts = Util::Data.prefix(group, contexts) 78 results = [ results, group_contexts ].flatten 79 end 80 81 return results 82 end
Return merged option groups for given context names.
This method allows us to easily request combinations of properties.
For example:
Nucleon::Config::Options.set(:context1, { :property1 => 'some value' }) Nucleon::Config::Options.set(:context2, { :property2 => 'another value' }) options = Nucleon::Config::Options.get([ :context1, :context2 ]) options = { :property1 => 'some value', :property2 => 'another value' }
-
Parameters
- Array<String, Symbol>, String, Symbol
-
contexts Context names to aggregate
- Boolean
-
force Force merge override if different types of data being merged
-
Returns
Hash
<Symbol|ANY>-
Aggregated context property collection
-
Errors
See also:
# File lib/core/config/options.rb 127 def self.get(contexts, force = true) 128 options = {} 129 130 unless contexts.is_a?(Array) 131 contexts = ( ! Util::Data.empty?(contexts) ? [ contexts ].flatten : [] ) 132 end 133 contexts.each do |name| 134 name = name.to_sym 135 if @@options.has_key?(name) 136 options = Util::Data.merge([ options, @@options[name] ], force, false) 137 end 138 end 139 return options 140 end
Assign property values to specified context identifiers.
This method allows us to easily merge properties across various contexts.
For example, see ::get
method
-
Parameters
- Array<String, Symbol>, String, Symbol
-
contexts Context names to assign properties
Hash
<String, Symbol|ANY>-
options Property collection to merge with existing properties
- Boolean
-
force Force merge override if different types of data being merged
-
Returns
- Void
-
This method does not currently have a return value
-
Errors
See also:
# File lib/core/config/options.rb 162 def self.set(contexts, options, force = true) 163 unless contexts.is_a?(Array) 164 contexts = ( ! Util::Data.empty?(contexts) ? [ contexts ].flatten : [] ) 165 end 166 contexts.each do |name| 167 name = name.to_sym 168 current_options = ( @@options.has_key?(name) ? @@options[name] : {} ) 169 @@options[name] = Util::Data.merge([ current_options, Config.symbol_map(options) ], force, false) 170 end 171 end