lib/xmlsimple.rb in xml-simple-1.1.4 vs lib/xmlsimple.rb in xml-simple-1.1.5
- old
+ new
@@ -9,11 +9,11 @@
# Easy API to maintain XML (especially configuration files).
class XmlSimple
include REXML
- @@VERSION = '1.1.4'
+ @@VERSION = '1.1.5'
# A simple cache for XML documents that were already transformed
# by xml_in.
class Cache
# Creates and initializes a new Cache object.
@@ -119,11 +119,11 @@
end
# Create a "global" cache.
@@cache = Cache.new
- # Creates and intializes a new XmlSimple object.
+ # Creates and initializes a new XmlSimple object.
#
# defaults::
# Default values for options.
def initialize(defaults = nil)
unless defaults.nil? || defaults.is_a?(Hash)
@@ -262,14 +262,14 @@
private
# Declare options that are valid for xml_in and xml_out.
KNOWN_OPTIONS = {
'in' => %w(
- keyattr keeproot forcecontent contentkey noattr
- searchpath forcearray suppressempty anonymoustag
- cache grouptags normalisespace normalizespace
- variables varattr keytosymbol attrprefix conversions
+ keyattr keeproot forcecontent contentkey noattr searchpath
+ forcearray suppressempty anonymoustag cache grouptags
+ normalisespace normalizespace variables varattr keytosymbol
+ attrtosymbol attrprefix conversions
),
'out' => %w(
keyattr keeproot contentkey noattr rootname
xmldeclaration outputfile noescape suppressempty
anonymoustag indent grouptags noindent attrprefix
@@ -283,15 +283,16 @@
DEF_XML_DECLARATION = "<?xml version='1.0' standalone='yes'?>"
DEF_ANONYMOUS_TAG = 'anon'
DEF_FORCE_ARRAY = true
DEF_INDENTATION = ' '
DEF_KEY_TO_SYMBOL = false
+ DEF_ATTR_TO_SYMBOL = false
# Normalizes option names in a hash, i.e., turns all
# characters to lower case and removes all underscores.
- # Additionally, this method checks, if an unknown option
- # was used and raises an according exception.
+ # Additionally, this method checks if an unknown option
+ # was used, and raises an according exception.
#
# options::
# Hash to be normalized.
# known_options::
# List of known options.
@@ -299,11 +300,11 @@
return nil if options.nil?
result = Hash.new
options.each { |key, value|
lkey = key.to_s.downcase.gsub(/_/, '')
if !known_options.member?(lkey)
- raise ArgumentError, "Unrecognised option: #{lkey}."
+ raise ArgumentError, "Unrecognized option: #{lkey}."
end
result[lkey] = value
}
result
end
@@ -351,10 +352,12 @@
@options['xmldeclaration'] = DEF_XML_DECLARATION
end
@options['keytosymbol'] = DEF_KEY_TO_SYMBOL unless @options.has_key?('keytosymbol')
+ @options['attrtosymbol'] = DEF_ATTR_TO_SYMBOL unless @options.has_key?('attrtosymbol')
+
if @options.has_key?('contentkey')
if @options['contentkey'] =~ /^-(.*)$/
@options['contentkey'] = $1
@options['collapseagain'] = true
end
@@ -708,12 +711,16 @@
# Document node to extract attributes from.
def get_attributes(node)
attributes = {}
if @options['attrprefix']
node.attributes.each { |n,v| attributes["@" + n] = v }
+ elsif @options.has_key?('attrtosymbol') and @options['attrtosymbol'] == true
+ #patch for converting attribute names to symbols
+ node.attributes.each { |n,v| attributes[n.to_sym] = v }
else
node.attributes.each { |n,v| attributes[n] = v }
end
+
attributes
end
# Determines, if a document element has mixed content.
#