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 Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_hashObject (readonly)

Returns the value of attribute config_hash.



18
19
20
# File 'lib/wright/config.rb', line 18

def config_hash
  @config_hash
end

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.



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

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



59
60
61
# File 'lib/wright/config.rb', line 59

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