Class: Wright::Config
- Inherits:
-
Object
- Object
- Wright::Config
- Extended by:
- Forwardable
- Defined in:
- lib/wright/config.rb
Overview
Public: Configuration container, wraps a regular Ruby hash.
Useful for getting and setting configuration values, such as logging verbosity, colour output and provider configuration.
Examples
Wright::Config[:foo] = { bar: :baz }
Wright::Config[:foo][:bar]
# => :baz
Class Method Summary collapse
-
.nested_key?(*path) ⇒ Boolean
Public: Check if a (nested) configuration value is set.
-
.nested_value(*path) ⇒ Object
Public: Retrieve a (nested) configuration value.
Class Method Details
.nested_key?(*path) ⇒ Boolean
Public: Check if a (nested) configuration value is set.
path - The configuration item as an argument list.
Examples
Wright::Config[:foo] = { bar: :baz }
Wright::Config.nested_key?(:foo, :bar)
# => true
Wright::Config.nested_key?(:this, :doesnt, :exist)
# => false
Returns 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
Public: Retrieve a (nested) configuration value.
path - The configuration item as an argument list.
Examples
Wright::Config[:foo] = { bar: :baz }
Wright::Config.nested_value(:foo, :bar)
# => :baz
Wright::Config.nested_value(:this, :doesnt, :exist)
# => nil
Returns the configuration value or nil if the value is not set.
60 61 62 |
# File 'lib/wright/config.rb', line 60 def self.nested_value(*path) nested_key?(*path) ? path.reduce(@config_hash) { |a, e| a[e] } : nil end |