lib/rfm/utilities/config.rb in ginjo-rfm-2.1.7 vs lib/rfm/utilities/config.rb in ginjo-rfm-3.0.0
- old
+ new
@@ -1,9 +1,16 @@
module Rfm
-
- # Should these go in Rfm module?
- CONFIG_KEYS = %w(
+ #
+ # 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'
+
+ CONFIG_KEYS = %w(
file_name
file_path
parser
host
port
@@ -24,21 +31,15 @@
log_parser
use
parent
grammar
field_mapping
+ capture_strings_with
+ logger
)
- 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'
+ CONFIG_DONT_STORE = %w(strings using parents symbols objects) #capture_strings_with)
extend self
@config = {}
# Set @config with args & options hash.
@@ -55,21 +56,21 @@
# == Pass a string as the first argument, to be used in the immediate context
# config 'my_layout' # in the model, to set model configuration
# Factory.layout 'my_layout', :my_group # to get a layout from settings in :my_group
#
def config(*args, &block)
- opt = args.rfm_extract_options!
@config ||= {}
- config_write(opt, args, &block)
+ return @config if args.empty?
+ config_write(*args, &block)
@config
end
# Sets @config just as above config method, but clears @config first.
def config_clear(*args)
- opt = args.rfm_extract_options!
@config = {}
- config_write(opt, args)
+ return @config if args.empty?
+ config_write(*args)
@config
end
# Reads compiled config, including filters and ad-hoc configuration params passed in.
# If first n parameters are strings, they will be appended to config[:strings].
@@ -82,38 +83,36 @@
#
# == Gets top level settings, merged with local and ad-hoc settings.
# get_config :layout => 'my_layout
#
def get_config(*arguments)
+ #puts caller_locations(1,1)[0]
args = arguments.clone
@config ||= {}
- opt = args.rfm_extract_options!
- 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
+ options = config_extract_options!(*args)
+ strings = options[:strings].rfm_force_array || []
+ symbols = options[:symbols].rfm_force_array.concat(options[:hash][:use].rfm_force_array) || []
+ objects = options[:objects].rfm_force_array || []
- rslt = config_merge_with_parent(symbols).merge(opt)
+ rslt = config_merge_with_parent(symbols).merge(options[:hash])
#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
+ def state(*args)
+ return @_state if args.empty? && !@state.nil? && (RUBY_VERSION[0,1].to_i > 1 ? (caller_locations(1,1) == @_last_state_caller) : false)
+ @_state = get_config(*args)
+ (@_last_state_caller = caller_locations(1,1)) if RUBY_VERSION[0,1].to_i > 1
+ @_state
+ end
+
+ def log
+ Rfm.log
+ end
protected
# Get or load a config file as the top-level config (above RFM_CONFIG constant).
# Default file name is rfm.yml.
@@ -128,57 +127,103 @@
config_file_paths.collect do |path|
(YAML.load_file(File.join(path, config_file_name)) rescue {})
end.inject({}){|h,a| h.merge(a)}
) || {}
end
+
+ # Get the top-level configuration from yml file and RFM_CONFIG
+ def get_config_base
+ get_config_file.merge((defined?(RFM_CONFIG) and RFM_CONFIG.is_a?(Hash)) ? RFM_CONFIG : {})
+ end
+
+ # Get the parent configuration object according to @config[:parent]
+ def get_config_parent
+ @config ||= {}
+ parent = case
+ when @config[:parent].is_a?(String); eval(@config[:parent])
+ when !@config[:parent].nil? && @config[:parent].is_a?(Rfm::Config); @config[:parent]
+ else eval('Rfm::Config')
+ end
+ end
-
# Merge args into @config, as :use=>[arg1, arg2, ...]
# 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).reject! {|k,v| CONFIG_DONT_STORE.include? k.to_s}
- yield(strings) if block_given?
+ # Pass in a block to use with parsed config in args.
+ def config_write(*args) #(opt, args)
+ options = config_extract_options!(*args)
+ options[:symbols].each{|a| @config.merge!(:use=>a.to_sym){|h,v1,v2| [v1].flatten << v2 }}
+ @config.merge!(options[:hash]).reject! {|k,v| CONFIG_DONT_STORE.include? k.to_s}
+ #options[:hash][:capture_strings_with].rfm_force_array.each do |label|
+ @config[:capture_strings_with].rfm_force_array.each do |label|
+ string = options[:strings].delete_at(0)
+ (@config[label] = string) if string && !string.empty?
+ end
+ parent = (options[:objects].delete_at(0) || options[:hash][:parent])
+ (@config[:parent] = parent) if parent
+ yield(options) if block_given?
end
# 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 {}
+ @config ||= {}
+
+ # Get upstream compilation
+ upstream = if (self != Rfm::Config)
+ #puts [self, @config[:parent], get_config_parent].join(', ')
+ get_config_parent.config_merge_with_parent
else
- get_config_file.merge((defined?(RFM_CONFIG) and RFM_CONFIG.is_a?(Hash)) ? RFM_CONFIG : {})
+ get_config_base
end.clone
-
- remote[:using] ||= []
- remote[:parents] ||= ['file', 'RFM_CONFIG']
+
+ upstream[:using] ||= []
+ upstream[: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 = config_filter(upstream, 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
+ rslt || {}
+# rescue
+# puts "Config#config_merge_with_parent for '#{self.class}' falied with #{$1}"
end
# 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)
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?
+ filters.each{|f| next unless conf[f]; conf.merge!(conf[f] || {})} if (!filters.nil? && !filters.empty?)
conf.delete(:use)
conf
end
-
- # This loads RFM_CONFIG into @config. It is not necessary,
- # as get_config will merge all configuration into RFM_CONFIG at runtime.
- #config RFM_CONFIG if defined? RFM_CONFIG
+ # Extract arguments into strings, symbols, objects, hash.
+ def config_extract_options!(*args)
+ strings, symbols, objects = [], [], []
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ args.each do |a|
+ case
+ when a.is_a?(String); strings << a
+ when a.is_a?(Symbol); symbols << a
+ else objects << a
+ end
+ end
+ {:strings=>strings, :symbols=>symbols, :objects=>objects, :hash=>options}
+ end
+
+ # Remove un-registered keys from a configuration hash.
+ # 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
+
end # module Config
end # module Rfm
\ No newline at end of file