Class: Google::Cloud::Config
- Inherits:
- BasicObject
- Defined in:
- lib/google/cloud/config.rb
Overview
Configuration mechanism for Google Cloud libraries. A Config object contains a list of predefined keys, some of which are values and others of which are subconfigurations, i.e. categories. Field access is generally validated to ensure that the field is defined, and when a a value is set, it is validated for the correct type. Warnings are printed when a validation fails.
You generally access fields and subconfigs by calling accessor methods.
Methods meant for "administration" such as adding options, are named
with a trailing "!" or "?" so they don't pollute the method namespace.
It is also possible to access a field using the []
operator.
Note that config objects inherit from BasicObject
. This means it does
not define many methods you might expect to find in most Ruby objects.
For example, to_s
, inspect
, is_a?
, instance_variable_get
, and so
forth.
Class Method Summary collapse
-
.config?(obj) ⇒ boolean
Determines if the given object is a config.
-
.create(show_warnings: true) {|config| ... } ⇒ Config
Constructs a Config object.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get the option or subconfig with the given name.
-
#[]=(key, value) ⇒ Object
Assign an option with the given name to the given value.
-
#add_alias!(key, to_key) ⇒ Object
Cause a key to be an alias of another key.
-
#add_config!(key, config = nil, &block) ⇒ Config
Add a subconfiguration field to this configuration.
-
#add_field!(key, initial = nil, opts = {}, &block) ⇒ Config
Add a value field to this configuration.
-
#alias?(key) ⇒ Symbol?
Check if the given key has been explicitly added as an alias.
-
#aliases! ⇒ Array<Symbol>
Return a list of alias names.
-
#delete!(key = nil) ⇒ Object
Remove the given key from the configuration, deleting any validation and value.
-
#field?(key) ⇒ boolean
Check if the given key has been explicitly added as a field name.
-
#fields! ⇒ Array<Symbol>
Return a list of explicitly added field names.
-
#reset!(key = nil) ⇒ Object
Restore the original default value of the given key.
-
#subconfig?(key) ⇒ boolean
Check if the given key has been explicitly added as a subconfig name.
-
#subconfigs! ⇒ Array<Symbol>
Return a list of explicitly added subconfig names.
-
#to_h! ⇒ Hash
Returns a nested hash representation of this configuration state, including subconfigs.
-
#to_s! ⇒ String
Returns a string representation of this configuration state, including subconfigs.
-
#value_set?(key) ⇒ boolean
(also: #option?)
Check if the given key has been set in this object.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Dynamic methods accessed as keys.
458 459 460 461 462 463 464 465 466 |
# File 'lib/google/cloud/config.rb', line 458 def method_missing name, *args name_str = name.to_s super unless name_str =~ /^[a-zA-Z]\w*=?$/ if name_str.chomp! "=" self[name_str] = args.first else self[name] end end |
Class Method Details
.config?(obj) ⇒ boolean
Determines if the given object is a config. Useful because Config
does not define the is_a?
method.
104 105 106 |
# File 'lib/google/cloud/config.rb', line 104 def self.config? obj Config.send :===, obj end |
.create(show_warnings: true) {|config| ... } ⇒ Config
Constructs a Config object. If a block is given, yields self
to the
block, which makes it convenient to initialize the structure by making
calls to add_field!
and add_config!
.
92 93 94 95 96 |
# File 'lib/google/cloud/config.rb', line 92 def self.create show_warnings: true config = new [], show_warnings: show_warnings yield config if block_given? config end |
Instance Method Details
#[](key) ⇒ Object
Get the option or subconfig with the given name.
309 310 311 312 313 314 315 316 317 |
# File 'lib/google/cloud/config.rb', line 309 def [] key key = resolve_key! key unless @validators.key? key warn! "Key #{key.inspect} does not exist. Returning nil." end value = @values[key] value = value.call if Config::DeferredValue === value value end |
#[]=(key, value) ⇒ Object
Assign an option with the given name to the given value.
297 298 299 300 301 |
# File 'lib/google/cloud/config.rb', line 297 def []= key, value key = resolve_key! key validate_value! key, @validators[key], value @values[key] = value end |
#add_alias!(key, to_key) ⇒ Object
Cause a key to be an alias of another key. The two keys will refer to the same field.
235 236 237 238 239 240 241 |
# File 'lib/google/cloud/config.rb', line 235 def add_alias! key, to_key key = validate_new_key! key @values.delete key @defaults.delete key @validators[key] = to_key.to_sym self end |
#add_config!(key, config = nil, &block) ⇒ Config
Add a subconfiguration field to this configuration.
You must provide a key, which becomes the method name that you use to navigate to the subconfig. Names may comprise only letters, numerals, and underscores, and must begin with a letter.
If you provide a block, the subconfig object is passed to the block, so you can easily add fields to the subconfig.
You may also pass in a config object that already exists. This will "attach" that configuration in this location.
218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/google/cloud/config.rb', line 218 def add_config! key, config = nil, &block key = validate_new_key! key if config.nil? config = Config.create(&block) elsif block yield config end @values[key] = config @defaults[key] = config @validators[key] = SUBCONFIG self end |
#add_field!(key, initial = nil, opts = {}, &block) ⇒ Config
Add a value field to this configuration.
You must provide a key, which becomes the field name in this config. Field names may comprise only letters, numerals, and underscores, and must begin with a letter. This will create accessor methods for the new configuration key.
You may pass an initial value (which defaults to nil if not provided).
You may also specify how values are validated. Validation is defined as follows:
- If you provide a block or a
:validator
option, it is used as the validator. A proposed value is passed to the proc, which should returntrue
orfalse
to indicate whether the value is valid. - If you provide a
:match
option, it is compared to the proposed value using the===
operator. You may, for example, provide a class, a regular expression, or a range. If you pass an array, the value is accepted if any of the elements match. - If you provide an
:enum
option, it should be anEnumerable
. A proposed value is valid if it is included. - Otherwise if you do not provide any of the above options, then a
default validation strategy is inferred from the initial value:
- If the initial is
true
orfalse
, then either boolean value is considered valid. This is the same asenum: [true, false]
. - If the initial is
nil
, then any object is considered valid. - Otherwise, any object of the same class as the initial value is
considered valid. This is effectively the same as
match: initial.class
.
- If the initial is
- You may also provide the
:allow_nil
option, which, if set to true, alters any of the above validators to allownil
values.
In many cases, you may find that the default validation behavior
(interpreted from the initial value) is sufficient. If you want to
accept any value, use match: Object
.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/google/cloud/config.rb', line 188 def add_field! key, initial = nil, opts = {}, &block key = validate_new_key! key opts[:validator] = block if block validator = resolve_validator! initial, opts validate_value! key, validator, initial @values[key] = initial @defaults[key] = initial @validators[key] = validator self end |
#alias?(key) ⇒ Symbol?
Check if the given key has been explicitly added as an alias. If so, return the target, otherwise return nil.
359 360 361 362 |
# File 'lib/google/cloud/config.rb', line 359 def alias? key target = @validators[key.to_sym] target.is_a?(::Symbol) ? target : nil end |
#aliases! ⇒ Array<Symbol>
Return a list of alias names.
387 388 389 |
# File 'lib/google/cloud/config.rb', line 387 def aliases! @validators.keys.find_all { |key| @validators[key].is_a? ::Symbol } end |
#delete!(key = nil) ⇒ Object
Remove the given key from the configuration, deleting any validation and value. If the key is omitted, delete all keys. If the key is an alias, deletes the alias but leaves the original.
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/google/cloud/config.rb', line 278 def delete! key = nil if key.nil? @values.clear @defaults.clear @validators.clear else @values.delete key @defaults.delete key @validators.delete key end self end |
#field?(key) ⇒ boolean
Check if the given key has been explicitly added as a field name.
338 339 340 |
# File 'lib/google/cloud/config.rb', line 338 def field? key @validators[key.to_sym].is_a? ::Proc end |
#fields! ⇒ Array<Symbol>
Return a list of explicitly added field names.
369 370 371 |
# File 'lib/google/cloud/config.rb', line 369 def fields! @validators.keys.find_all { |key| @validators[key].is_a? ::Proc } end |
#reset!(key = nil) ⇒ Object
Restore the original default value of the given key. If the key is omitted, restore the original defaults for all keys, and all keys of subconfigs, recursively.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/google/cloud/config.rb', line 251 def reset! key = nil if key.nil? @values.each_key { |k| reset! k } else key = key.to_sym if @defaults.key? key @values[key] = @defaults[key] @values[key].reset! if @validators[key] == SUBCONFIG elsif @values.key? key warn! "Key #{key.inspect} has not been added, but has a value." \ " Removing the value." @values.delete key else warn! "Key #{key.inspect} does not exist. Nothing to reset." end end self end |
#subconfig?(key) ⇒ boolean
Check if the given key has been explicitly added as a subconfig name.
348 349 350 |
# File 'lib/google/cloud/config.rb', line 348 def subconfig? key @validators[key.to_sym] == SUBCONFIG end |
#subconfigs! ⇒ Array<Symbol>
Return a list of explicitly added subconfig names.
378 379 380 |
# File 'lib/google/cloud/config.rb', line 378 def subconfigs! @validators.keys.find_all { |key| @validators[key] == SUBCONFIG } end |
#to_h! ⇒ Hash
Returns a nested hash representation of this configuration state, including subconfigs. Only explicitly added fields and subconfigs are included.
413 414 415 416 417 418 419 420 |
# File 'lib/google/cloud/config.rb', line 413 def to_h! h = {} @validators.each_key do |k| v = @values[k] h[k] = Config.config?(v) ? v.to_h! : v.inspect end h end |
#to_s! ⇒ String
Returns a string representation of this configuration state, including subconfigs. Only explicitly added fields and subconfigs are included.
397 398 399 400 401 402 403 404 |
# File 'lib/google/cloud/config.rb', line 397 def to_s! elems = @validators.keys.map do |k| v = @values[k] vstr = Config.config?(v) ? v.to_s! : value.inspect " #{k}=#{vstr}" end "<Config:#{elems.join}>" end |
#value_set?(key) ⇒ boolean Also known as: option?
Check if the given key has been set in this object. Returns true if the key has been added as a normal field, subconfig, or alias, or if it has not been added explicitly but still has a value.
327 328 329 |
# File 'lib/google/cloud/config.rb', line 327 def value_set? key @values.key? resolve_key! key end |