lib/mixlib/config.rb in mixlib-config-1.1.0.rc01 vs lib/mixlib/config.rb in mixlib-config-1.1.0
- old
+ new
@@ -16,10 +16,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
+class Object # http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html
+ def meta_def name, &blk
+ (class << self; self; end).instance_eval { define_method name, &blk }
+ end
+end
+
module Mixlib
module Config
def self.extended(base)
class << base; attr_accessor :configuration; end
@@ -117,11 +123,11 @@
# method_symbol<Symbol>:: Name of the method (variable setter)
# value<Object>:: Value to be set in config hash
#
def internal_set(method_symbol,value)
method_name = method_symbol.id2name
- if self.respond_to?("#{method_name}=".to_sym)
+ if (self.public_methods - ["[]="]).include?("#{method_name}=")
self.send("#{method_name}=", value)
else
self.configuration[method_symbol] = value
end
end
@@ -134,12 +140,11 @@
# method_symbol<Symbol>:: Name of the method (variable setter)
# blk<Block>:: logic block to run in setting slot method_symbol to value
# value<Object>:: Value to be set in config hash
#
def config_attr_writer(method_symbol, &blk)
- meta = class << self; self; end
- method_name = "#{method_symbol.to_s}=".to_sym
- meta.send :define_method, method_name do |value|
+ method_name = "#{method_symbol.to_s}="
+ meta_def method_name do |value|
self.configuration[method_symbol] = blk.call(value)
end
end
# Allows for simple lookups and setting of configuration options via method calls