Class: Wright::Config

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/wright/config.rb

Overview

Configuration container, wraps a regular Ruby hash.

Useful for getting and setting configuration values, such as logging verbosity, color output and provider configuration.

Examples:

Wright::Config[:foo] = { bar: :baz }
Wright::Config[:foo][:bar]
# => :baz

Class Method Summary collapse

Class Method Details

.nested_key?(*path) ⇒ Bool

Checks if a (nested) configuration value is set.

Examples:

Wright::Config[:foo] = { bar: :baz }
Wright::Config.nested_key?(:foo, :bar)
# => true

Wright::Config.nested_key?(:this, :doesnt, :exist)
# => false

Parameters:

  • path (Array<Symbol>)

    the configuration key

Returns:

  • (Bool)

    true if the configuration value is set and false otherwise.


35
36
37
38
39
40
41
42
# File 'lib/wright/config.rb', line 35

def self.nested_key?(*path)
  last_key = path.pop
  last_hash = path.reduce(@config_hash) do |hash, key|
    return false unless hash.respond_to?(:fetch)
    hash.fetch(key, {})
  end
  last_hash.respond_to?(:key?) && last_hash.key?(last_key)
end

.nested_value(*path) ⇒ Object

Retrieves a (nested) configuration value.

Examples:

Wright::Config[:foo] = { bar: :baz }
Wright::Config.nested_value(:foo, :bar)
# => :baz

Wright::Config.nested_value(:this, :doesnt, :exist)
# => nil

Parameters:

  • path (Array<Symbol>)

    the configuration key

Returns:

  • the configuration value or nil if the value is not set


57
58
59
# File 'lib/wright/config.rb', line 57

def self.nested_value(*path)
  nested_key?(*path) ? path.reduce(@config_hash) { |a, e| a[e] } : nil
end