Module: VagrantPlugins::ProxyConf::Config::KeyMixin
Overview
Helper module for Config classes.
Handles mapping to environment variables, setting default values, and constructing the configuration file stanza.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
Extends the including class with ClassMethods.
Instance Method Summary collapse
-
#config_for(key, value) ⇒ #to_s
Returns a configuration line/stanza for the specified key and value.
-
#enabled? ⇒ Boolean
True if any of the configuration keys has a non-nil value.
-
#finalize! ⇒ Object
Overrides values from specified environment variables, and sets them to default values if no configuration was found.
- #get(key) ⇒ Object protected
-
#initialize ⇒ Object
Initializes all keys to ‘UNSET_VALUE`.
- #key?(key) ⇒ Boolean protected
- #keys ⇒ Object protected
-
#merge_defaults(defaults) ⇒ KeyMixin
Returns a new instance of this class where all nil keys are replaced from the specified default config.
- #set(key, value) ⇒ Object protected
- #set?(key) ⇒ Boolean protected
-
#to_s ⇒ String
Returns the full configuration stanza Calls #config_for for each key.
Class Method Details
.included(base) ⇒ Object
Extends the including class with ClassMethods
31 32 33 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 31 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#config_for(key, value) ⇒ #to_s
Returns a configuration line/stanza for the specified key and value. The returned line should include linefeed ‘\n` if not empty. The default implementations returns “<key>=<value>\n”.
70 71 72 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 70 def config_for(key, value) "#{key}=#{value}\n" end |
#enabled? ⇒ Boolean
Returns true if any of the configuration keys has a non-nil value.
51 52 53 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 51 def enabled? keys.any? { |key| set?(key) } end |
#finalize! ⇒ Object
Overrides values from specified environment variables, and sets them to default values if no configuration was found
45 46 47 48 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 45 def finalize! super keys.each { |key| set(key, resolve_value(key)) } end |
#get(key) ⇒ Object (protected)
99 100 101 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 99 def get(key) send(key.name) end |
#initialize ⇒ Object
Initializes all keys to ‘UNSET_VALUE`
36 37 38 39 40 41 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 36 def initialize super keys.each do |key| set(key, self.class::UNSET_VALUE) end end |
#key?(key) ⇒ Boolean (protected)
95 96 97 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 95 def key?(key) keys.any? { |k| k.name == key.name } end |
#keys ⇒ Object (protected)
91 92 93 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 91 def keys self.class.keys end |
#merge_defaults(defaults) ⇒ KeyMixin
Returns a new instance of this class where all nil keys are replaced from the specified default config
79 80 81 82 83 84 85 86 87 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 79 def merge_defaults(defaults) result = dup keys.each do |key| if !set?(key) && defaults.key?(key) result.set(key, defaults.get(key)) end end result end |
#set(key, value) ⇒ Object (protected)
107 108 109 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 107 def set(key, value) send(:"#{key.name}=", value) end |
#set?(key) ⇒ Boolean (protected)
103 104 105 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 103 def set?(key) !get(key).nil? end |
#to_s ⇒ String
Returns the full configuration stanza Calls #config_for for each key.
59 60 61 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 59 def to_s keys.map { |key| config_for(key, get(key)).to_s }.join end |