From fa52964f684b2afa39d383cc2639a5a2a53c36c5 Mon Sep 17 00:00:00 2001 From: Bernerd Schaefer Date: Sat, 17 Jul 2010 16:45:19 -0500 Subject: [PATCH] XmlMini.rename_key emits valid xml with dasherize This resolves issues for libraries which use '_' prefixed keys in their attributes hash, such as Mongoid. A key like "_id" or "_type" will no longer be converted to "<-id>" and "<-type>". --- activesupport/lib/active_support/xml_mini.rb | 6 +++- activesupport/test/test_xml_mini.rb | 49 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletions(-) create mode 100644 activesupport/test/test_xml_mini.rb diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 7594d7b..aed5ba6 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -129,12 +129,16 @@ module ActiveSupport camelize = options.has_key?(:camelize) && options[:camelize] dasherize = !options.has_key?(:dasherize) || options[:dasherize] key = key.camelize if camelize - key = key.dasherize if dasherize + key = _dasherize(key) if dasherize key end protected + def _dasherize(key) + key.gsub(/(?!^[_]*)_(?![_]*$)/, '-') + end + # TODO: Add support for other encodings def _parse_binary(bin, entity) #:nodoc: case entity['encoding'] diff --git a/activesupport/test/test_xml_mini.rb b/activesupport/test/test_xml_mini.rb new file mode 100644 index 0000000..585eb15 --- /dev/null +++ b/activesupport/test/test_xml_mini.rb @@ -0,0 +1,49 @@ +require 'abstract_unit' +require 'active_support/xml_mini' + +class XmlMiniTest < Test::Unit::TestCase + def test_rename_key_dasherizes_by_default + assert_equal "my-key", ActiveSupport::XmlMini.rename_key("my_key") + end + + def test_rename_key_does_nothing_with_dasherize_true + assert_equal "my-key", ActiveSupport::XmlMini.rename_key("my_key", :dasherize => true) + end + + def test_rename_key_does_nothing_with_dasherize_false + assert_equal "my_key", ActiveSupport::XmlMini.rename_key("my_key", :dasherize => false) + end + + def test_rename_key_camelizes_with_camelize_true + assert_equal "MyKey", ActiveSupport::XmlMini.rename_key("my_key", :camelize => true) + end + + def test_rename_key_camelizes_with_camelize_true + assert_equal "MyKey", ActiveSupport::XmlMini.rename_key("my_key", :camelize => true) + end + + def test_rename_key_does_not_dasherize_leading_underscores + assert_equal "_id", ActiveSupport::XmlMini.rename_key("_id") + end + + def test_rename_key_with_leading_underscore_dasherizes_interior_underscores + assert_equal "_my-key", ActiveSupport::XmlMini.rename_key("_my_key") + end + + def test_rename_key_does_not_dasherize_trailing_underscores + assert_equal "id_", ActiveSupport::XmlMini.rename_key("id_") + end + + def test_rename_key_with_trailing_underscore_dasherizes_interior_underscores + assert_equal "my-key_", ActiveSupport::XmlMini.rename_key("my_key_") + end + + def test_rename_key_does_not_dasherize_multiple_leading_underscores + assert_equal "__id", ActiveSupport::XmlMini.rename_key("__id") + end + + def test_rename_key_does_not_dasherize_multiple_leading_underscores + assert_equal "id__", ActiveSupport::XmlMini.rename_key("id__") + end + +end -- 1.7.1