class R10K::Settings::Container
Defines a collection for application settings
This implements a hierarchical interface to application settings. Containers can define an optional parent container that will be used for default options if those options aren't set on the given container.
Attributes
@!attribute [r] #valid_keys
@return [Set<Symbol>] All valid keys defined on the container or parent container.
Public Class Methods
@param parent [R10K::Settings::Container] An optional parent container
# File lib/r10k/settings/container.rb, line 13 def initialize(parent = nil) @parent = parent @valid_keys = Set.new @settings = {} end
Public Instance Methods
Look up a value in the container. The lookup checks the current container, and then falls back to the parent container if it's given.
@param key [Symbol] The lookup key
@return [Object, nil] The retrieved value if present.
@raise [R10K::Settings::Container::InvalidKey] If the looked up key isn't
a valid key.
# File lib/r10k/settings/container.rb, line 29 def [](key) validate_key! key if @settings[key] @settings[key] elsif @parent && (pkey = @parent[key]) @settings[key] = pkey.dup @settings[key] end end
Set a value on the container
@param key [Symbol] The lookup key @param value [Object] The value to store in the container
@raise [R10K::Settings::Container::InvalidKey] If the looked up key isn't
a valid key.
# File lib/r10k/settings/container.rb, line 47 def []=(key, value) validate_key! key @settings[key] = value end
Define a valid container key
@note This should only be used by {#R10K::Settings::ClassSettings}
@param key [Symbol] @return [void]
# File lib/r10k/settings/container.rb, line 59 def add_valid_key(key) @valid_keys.add(key) end
Clear all existing settings in this container. Valid settings are left alone. @return [void]
# File lib/r10k/settings/container.rb, line 79 def reset! @settings = {} end
Determine if a key is a valid setting.
@param key [Symbol]
@return [true, false]
# File lib/r10k/settings/container.rb, line 68 def valid_key?(key) if @valid_keys.include?(key) true elsif @parent and @parent.valid_key?(key) @valid_keys.add(key) true end end
Private Instance Methods
# File lib/r10k/settings/container.rb, line 85 def validate_key!(key) unless valid_key?(key) raise InvalidKey, "Key #{key} is not a valid key" end end