Permalink
Browse files

Added reference support for xml files.

  • Loading branch information...
1 parent f237d69 commit 76d704d497ca793fb4e67e9f6fa8c2af1f8bf41e @rahmal committed Apr 16, 2012
Showing with 25 additions and 41 deletions.
  1. +1 −1 ChangeLog
  2. +2 −1 lib/rconfig.rb
  3. +8 −33 lib/rconfig/config.rb
  4. +1 −1 lib/rconfig/properties_file.rb
  5. +13 −5 lib/rconfig/utils.rb
View
@@ -13,7 +13,7 @@
* Fixed minor typos, comments, and bugs.
* Refactored Hash#weave signature; changed dont_clobber=true, to clobber=false (reads more intuitively).
* Removed redundant validate method from PropertiesFileParser.
-* Moved PropertiesFileParser regexps to constants.
+* Renamed PropertiesFileParser to PropertiesFile
0.3.3
=====
View
@@ -88,7 +88,8 @@ module RConfig
autoload :Callbacks, 'rconfig/callbacks'
autoload :Reload, 'rconfig/reload'
autoload :CoreMethods, 'rconfig/core_methods'
- autoload :PropertiesFileParser, 'rconfig/properties_file_parser'
+ autoload :PropertiesFile, 'rconfig/properties_file'
+ autoload :InstallGenerator, 'generators/rconfig/install_generator'
extend ActiveSupport::Concern
View
@@ -7,7 +7,7 @@
# embedded hash values, similar to the way one might traverse a object tree.
#
module RConfig
- class Config < HashWithIndifferentAccess
+ class Config < ::HashWithIndifferentAccess
##
# HashWithIndifferentAccess#default is broken in early versions of Rails.
@@ -41,6 +41,7 @@ class Config < HashWithIndifferentAccess
# This also works inside the composite array keys.
def method_missing(method, *args)
method = method.to_s
+ return if method == 'default_key'
value = self[method]
case args.size
when 0 # e.g.: RConfig.application.method
@@ -52,45 +53,19 @@ def method_missing(method, *args)
end
end
- ##
- # HashWithIndifferentAccess#dup always returns HashWithIndifferentAccess!
- # This is overriden to return Config (or any other derived class)
- def dup
- self.class.new(self)
- end
-
- ##
- # Why the &*#^@*^&$ isn't HashWithIndifferentAccess doing this?
- # HashWithIndifferentAccess doesn't (or didn't?) override Hash's
- # []! That's why it's so destructive!
- def [](key)
- key = key.to_s if key.kind_of?(Symbol)
- super(key)
- end
-
##
# Allow hash.default => hash['default']
# without breaking Hash's usage of default(key)
def default(key = self.default_key)
key = key.to_s if key.is_a?(Symbol)
- key == self.default_key ? self['default'] : hash_default(key)
+ if key == self.default_key
+ self['default'] if key?('default')
+ else
+ hash_default(key)
+ end
end
- ##
- # HashWithIndifferentAccess#update is broken in earlier versions of Rails
- # Hash#update returns self, BUT, HashWithIndifferentAccess#update does not!
- #
- # { :a => 1 }.update({ :b => 2, :c => 3 })
- # => { :a => 1, :b => 2, :c => 3 }
- #
- # HashWithIndifferentAccess.new({ :a => 1 }).update({ :b => 2, :c => 3 })
- # => { :b => 2, :c => 3 } # WTF?
- #
- # Subclasses should *never* override methods and break their protocols!!!!
- def update(hash)
- super(hash)
- self
- end
+ protected
##
# Override HashWithIndifferentAccess#convert_value
@@ -54,7 +54,7 @@ def self.parse(config_file)
# The config is top down.. anything after a [group] gets added as part
# of that group until a new [group] is found.
group, topgrp = nil
- config_file.each do |line| # for each line in the config file
+ config_file.split("\n").each do |line| # for each line in the config file
line.strip!
unless COMMENT.match(line) # skip comments (lines that state with '#')
if KEYVAL.match(line) # if this line is a config property
View
@@ -91,15 +91,23 @@ def parse(contents, name, ext)
when *YML_FILE_TYPES
YAML::load(contents)
when *XML_FILE_TYPES
- Hash.from_xml(contents)[name] # xml document must have root tag matching the file name.
+ parse_xml(contents, name)
when *CNF_FILE_TYPES
- PropertiesFile.parse(contents)
+ RConfig::PropertiesFile.parse(contents)
else
raise ConfigError, "Unknown File type: #{ext}"
end
hash.freeze
end
+ ##
+ # Parses xml file and processes any references in the property values.
+ def parse_xml(contents, name)
+ hash = Hash.from_xml(contents)
+ hash = hash[name] if hash.size == 1 && hash.key?(name) # xml document could have root tag matching the file name.
+ RConfig::PropertiesFile.parse_references(hash)
+ end
+
##
# Returns a merge of hashes.
#
@@ -144,7 +152,7 @@ def flush_cache(name=nil)
name = name.to_s
self.cache_hash[name] &&= nil
else
- logger.warn "Flushing complete config data cache."
+ logger.warn "RConfig: Flushing config data cache."
self.suffixes = {}
self.cache = {}
self.cache_files = {}
@@ -157,8 +165,8 @@ def flush_cache(name=nil)
##
# Get complete file name, including file path for the given config name
# and directory.
- def filename_for_name(name, dir=self.load_paths.first, ext=:yml)
- File.join(dir, "#{name}.#{ext}")
+ def filename_for_name(name, directory=self.load_paths.first, ext=:yml)
+ File.join(directory, "#{name}.#{ext}")
end

0 comments on commit 76d704d

Please sign in to comment.