lib/configurability.rb in configurability-2.2.2 vs lib/configurability.rb in configurability-2.3.0.pre20161121123955

- old
+ new

@@ -19,11 +19,11 @@ # Library version constant VERSION = '2.2.2' # Version-control revision constant - REVISION = %q$Revision: 806d1f512f55 $ + REVISION = %q$Revision: 93764baee5f8 $ require 'configurability/deferredconfig' autoload :Config, 'configurability/config' @@ -124,42 +124,50 @@ end ### Install the appropriate section of the +config+ into the given +object+. def self::install_config( config, object ) - section = object.config_key.to_sym - self.log.debug "Configuring %p with the %p section of the config." % - [ object, section ] + self.log.debug "Configuring %p with the %s section of the config." % + [ object, object.config_key ] - if config.respond_to?( section ) - self.log.debug " config has a %p method; using that" % [ section ] - section = config.send( section ) + section = self.find_config_section( config, object.config_key ) + configure_method = object.method( :configure ) + + self.log.debug " calling %p" % [ configure_method ] + configure_method.call( section ) + end + + + ### Find the section of the specified +config+ object that corresponds to the + ### given +key+. + def self::find_config_section( config, key ) + return key.to_s.split( '__' ).inject( config ) do |section, subkey| + next nil if section.nil? + self.get_config_subsection( section, subkey.to_sym ) + end + end + + + ### Return the subsection of the specified +config+ that corresponds to +key+, trying + ### both struct-like and hash-like interfaces. + def self::get_config_subsection( config, key ) + if config.respond_to?( key ) + self.log.debug " config has a #%s method; using that" % [ key ] + return config.send( key ) elsif config.respond_to?( :[] ) && config.respond_to?( :key? ) self.log.debug " config has a hash-ish interface..." - if config.key?( section ) || config.key?( section.to_s ) - self.log.debug " and has a %p member; using that" % [ section ] - section = config[section] || config[section.to_s] + if config.key?( key.to_sym ) || config.key?( key.to_s ) + self.log.debug " and has a %s member; using that" % [ key ] + return config[ key.to_sym ] || config[ key.to_s ] else - self.log.debug " but no %p member." - section = nil + self.log.debug " but no `%s` member." % [ key ] + return nil end else - self.log.debug " no %p section in %p; configuring with nil" % [ section, config ] - section = nil + self.log.debug " no %p section in %p; configuring with nil" % [ key, config ] + return nil end - - # Figure out if the configure method has already been called with this config - # section before, and don't re-call it if so - configure_method = object.method( :configure ) - - if self.configured[ configure_method ] == section - self.log.debug " avoiding re-calling %p" % [ configure_method ] - return - end - - self.log.debug " calling %p" % [ configure_method ] - configure_method.call( section ) end ### Gather defaults from objects with Configurability in the given +collection+ ### object. Objects that wish to add a section to the defaults should implement @@ -208,16 +216,22 @@ ### Set the config key of the object. def config_key=( sym ) Configurability.configurable_objects |= [ self ] - @config_key = sym + @config_key = normalize_config_key( sym ) end ### Default configuration method. def configure( config ) @config = config + end + + + ### Return the specified +key+ normalized into a valid Symbol config key. + def normalize_config_key( key ) + return key.to_s.gsub( /\./, '__' ).to_sym end # # :section: Configuration Defaults API