Module: VagrantPlugins::ProxyConf::Config::KeyMixin
- Defined in:
- lib/vagrant-proxyconf/config/key_mixin.rb
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
33 34 35 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 33 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”.
72 73 74 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 72 def config_for(key, value) "#{key}=#{value && value}\n" end |
#enabled? ⇒ Boolean
Returns true if any of the configuration keys has a non-nil value.
53 54 55 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 53 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
47 48 49 50 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 47 def finalize! super keys.each { |key| set(key, resolve_value(key)) } end |
#get(key) ⇒ Object (protected)
101 102 103 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 101 def get(key) public_send(key.name) end |
#initialize ⇒ Object
Initializes all keys to ‘UNSET_VALUE`
38 39 40 41 42 43 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 38 def initialize super keys.each do |key| set(key, self.class::UNSET_VALUE) end end |
#key?(key) ⇒ Boolean (protected)
97 98 99 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 97 def key?(key) keys.any? { |k| k.name == key.name } end |
#keys ⇒ Object (protected)
93 94 95 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 93 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
81 82 83 84 85 86 87 88 89 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 81 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)
109 110 111 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 109 def set(key, value) public_send(:"#{key.name}=", value) end |
#set?(key) ⇒ Boolean (protected)
105 106 107 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 105 def set?(key) !get(key).nil? end |
#to_s ⇒ String
Returns the full configuration stanza Calls #config_for for each key.
61 62 63 |
# File 'lib/vagrant-proxyconf/config/key_mixin.rb', line 61 def to_s keys.map { |key| config_for(key, get(key)).to_s }.join end |