Module: Plist4r::Mixlib::Config
- Included in:
- Plist4r::Config
- Defined in:
- lib/plist4r/mixin/mixlib_config.rb
Instance Methods
Overview
Author:: Adam Jacob (<adam@opscode.com>) Author:: Nuo Yan (<nuo@opscode.com>) Author:: Christopher Brown (<cb@opscode.com>) Copyright:: Copyright (c) 2008 Opscode, Inc. License:: Apache License, Version 2.0 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing and limitations under the License.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(method_symbol, *args)
Allows for simple lookups and setting of configuration options via method calls on Mixlib::Config. If there any arguments to the method, they are used to set the value of the configuration option. Otherwise, it’s a simple get operation.
Parameters
method_symbol | The method called. Must match a configuration option. |
*args: | Any arguments passed to the method |
Returns
value: | The value of the configuration option. |
Raises
If the method_symbol does not match a configuration option. |
170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 170 def method_missing(method_symbol, *args) num_args = args.length # Setting if num_args > 0 method_symbol = $1.to_sym unless (method_symbol.to_s =~ /(.+)=$/).nil? internal_set method_symbol, (num_args == 1 ? args[0] : args) end # Returning self.configuration[method_symbol] end |
Instance Method Details
- (Object) [](config_option)
Get the value of a configuration option
Parameters
config_option | The configuration option to return |
Returns
value: | The value of the configuration option |
Raises
If the configuration option does not exist |
70 71 72 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 70 def [](config_option) self.configuration[config_option.to_sym] end |
- (Object) []=(config_option, value)
Set the value of a configuration option
Parameters
config_option | The configuration option to set (within the []) |
value: | The value for the configuration option |
Returns
value: | The new value of the configuration option |
82 83 84 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 82 def []=(config_option, value) internal_set(config_option,value) end |
- (Object) config_attr_writer(method_symbol, &blk)
metaprogramming to ensure that the slot for method_symbol gets set to value after any other logic is run
Parameters
method_symbol | Name of the method (variable setter) |
blk | logic block to run in setting slot method_symbol to value |
value | Value to be set in config hash |
150 151 152 153 154 155 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 150 def config_attr_writer(method_symbol, &blk) method_name = "#{method_symbol.to_s}=" method_name do |value| self.configuration[method_symbol] = blk.call(value) end end |
- (Object) configure(&block)
Pass Mixlib::Config.configure() a block, and it will yield self.configuration.
Parameters
A block that is sent self.configuration as its argument |
56 57 58 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 56 def configure(&block) block.call(self.configuration) end |
- (Object) from_file(filename)
Loads a given ruby file, and runs instance_eval against it in the context of the current object.
Raises an IOError if the file cannot be found, or is not readable.
Parameters
A filename to read from |
48 49 50 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 48 def from_file(filename) self.instance_eval(IO.read(filename), filename, 1) end |
- (Boolean) has_key?(key)
Check if Mixlib::Config has a configuration option.
Parameters
key | The configuration option to check for |
Returns
If the configuration option exists | |
If the configuration option does not exist |
94 95 96 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 94 def has_key?(key) self.configuration.has_key?(key.to_sym) end |
- (Object) hash_dup
Creates a shallow copy of the internal hash
Returns
result of Hash#dup
121 122 123 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 121 def hash_dup self.configuration.dup end |
- (Object) keys
Return the set of config hash keys
Returns
result of Hash#keys
113 114 115 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 113 def keys self.configuration.keys end |
- (Object) merge!(hash)
Merge an incoming hash with our config options
Parameters
hash | The incoming hash |
Returns
result of Hash#merge!
105 106 107 |
# File 'lib/plist4r/mixin/mixlib_config.rb', line 105 def merge!(hash) self.configuration.merge!(hash) end |