lib/rfm/utilities/config.rb in ginjo-rfm-2.0.2 vs lib/rfm/utilities/config.rb in ginjo-rfm-2.1.0.pre02

- old
+ new

@@ -1,15 +1,8 @@ module Rfm - # Top level config hash accepts any defined config parameters, - # or group-name keys pointing to config subsets. - # The subsets can be any grouping of defined config parameters, as a hash. - # See CONFIG_KEYS for defined config parameters. - # - module Config - require 'yaml' - + # Should these go in Rfm module? CONFIG_KEYS = %w( file_name file_path parser host @@ -29,14 +22,25 @@ log_actions log_responses log_parser use parent + grammar ) + + CONFIG_DONT_STORE = %w(strings using parents symbols objects) + # Top level config hash accepts any defined config parameters, + # or group-name keys pointing to config subsets. + # The subsets can be any grouping of defined config parameters, as a hash. + # See CONFIG_KEYS for defined config parameters. + # + module Config + require 'yaml' + extend self - @config = {} + @config = {} # Set @config with args & options hash. # Args should be symbols representing configuration groups, # with optional config hash as last arg, to be merged on top. # Returns @config. @@ -76,21 +80,39 @@ # get_config :my_server_group, :layout => 'my_layout' # This gets top level settings, # # == Gets top level settings, merged with local and ad-hoc settings. # get_config :layout => 'my_layout # - def get_config(*args) + def get_config(*arguments) + args = arguments.clone @config ||= {} opt = args.rfm_extract_options! - strings = [] - while args[0].is_a?(String) do; strings << args.shift; end - if args.size == 0 - config_filter(config_merge_with_parent) - else - config_filter(config_merge_with_parent, args) - end.merge(opt).merge(:strings=>strings) + strings = opt[:strings].rfm_force_array || [] + symbols = opt[:use].rfm_force_array || [] + objects = opt[:objects].rfm_force_array || [] + args.each do |arg| + case true + when arg.is_a?(String) ; strings << arg + when arg.is_a?(Symbol) ; symbols << arg + else objects.unshift arg + end + end + + rslt = config_merge_with_parent(symbols).merge(opt) + #using = rslt[:using].rfm_force_array + sanitize_config(rslt, CONFIG_DONT_STORE, false) + rslt[:using].delete "" + rslt[:parents].delete "" + rslt.merge(:strings=>strings, :objects=>objects) end + + # Keep should be a list of strings representing keys to keep. + def sanitize_config(conf={}, keep=[], dupe=false) + (conf = conf.clone) if dupe + conf.reject!{|k,v| (!CONFIG_KEYS.include?(k.to_s) or [{},[],''].include?(v)) and !keep.include? k.to_s } + conf + end protected # Get or load a config file as the top-level config (above RFM_CONFIG constant). # Default file name is rfm.yml. @@ -113,40 +135,42 @@ # Then merge optional config hash into @config. # Pass in a block to use with strings in args. See base.rb. def config_write(opt, args) strings = []; while args[0].is_a?(String) do; strings << args.shift; end args.each{|a| @config.merge!(:use=>a.to_sym)} - @config.merge!(opt) + @config.merge!(opt).reject! {|k,v| CONFIG_DONT_STORE.include? k.to_s} yield(strings) if block_given? end - # Get composite config from all levels, adding :use parameters to a - # temporary top-level value. - def config_merge_with_parent + # Get composite config from all levels, processing :use parameters at each level + def config_merge_with_parent(filters=nil) remote = if (self != Rfm::Config) eval(@config[:parent] || 'Rfm::Config').config_merge_with_parent rescue {} else get_config_file.merge((defined?(RFM_CONFIG) and RFM_CONFIG.is_a?(Hash)) ? RFM_CONFIG : {}) - end + end.clone - use = (remote[:use].rfm_force_array | @config[:use].rfm_force_array).compact - remote.merge(@config).merge(:use=>use) + remote[:using] ||= [] + remote[:parents] ||= ['file', 'RFM_CONFIG'] + + filters = (@config[:use].rfm_force_array | filters.rfm_force_array).compact + rslt = config_filter(remote, filters).merge(config_filter(@config, filters)) + + rslt[:using].concat((@config[:use].rfm_force_array | filters).compact.flatten) #.join + rslt[:parents] << @config[:parent].to_s + + rslt.delete :parent + + rslt end - # This version uses either/or method input filters OR compiled config :use=>filters. - # Given config hash, return filtered subgroup settings. Filters should be symbols. - # def config_filter(conf, filters=nil) - # filters ||= conf[:use].rfm_force_array if !conf[:use].blank? - # filters.each{|f| next unless conf[f]; conf.merge!(conf[f] || {})} if !filters.blank? - # conf.reject!{|k,v| !CONFIG_KEYS.include?(k.to_s) or v.to_s == '' } - # conf - # end - # - # This version combines both method input filters AND :use=>filters + # Returns a configuration hash overwritten by :use filters in the hash + # that match passed-in filter names or any filter names contained within the hash's :use key. def config_filter(conf, filters=nil) - filters = conf[:use] = (conf[:use].rfm_force_array | filters.rfm_force_array).compact + conf = conf.clone + filters = (conf[:use].rfm_force_array | filters.rfm_force_array).compact filters.each{|f| next unless conf[f]; conf.merge!(conf[f] || {})} if !filters.blank? - conf.reject!{|k,v| !CONFIG_KEYS.include?(k.to_s) or [{},[],''].include?(v) } + conf.delete(:use) conf end # This loads RFM_CONFIG into @config. It is not necessary, \ No newline at end of file