xml_serialization 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,71 @@
1
+ # Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.
2
+
3
+ require 'rubygems'
4
+ require 'activesupport'
5
+ require 'builder'
6
+
7
+ module SimpleSerializer
8
+ def to_xml(options={})
9
+ builder = options[:builder] || Builder::XmlMarkup.new(:indent => options[:indent])
10
+ tag = options[:root] || self.class.name.downcase
11
+ dasherize = ! options.has_key?(:dasherize) || options[:dasherize]
12
+ tag = dasherize ? tag.to_s.dasherize : tag
13
+ builder.tag!(tag, self.to_s)
14
+ end
15
+ end
16
+
17
+ class String
18
+ include SimpleSerializer
19
+
20
+ XML_HEAD_PATTERN = /\A\s*<\?xml[^>]*>\s*/m
21
+ def strip_xml_head!
22
+ sub!(XML_HEAD_PATTERN, '')
23
+ end
24
+
25
+ def strip_xml_head
26
+ sub(XML_HEAD_PATTERN, '')
27
+ end
28
+ end
29
+
30
+ class Symbol
31
+ include SimpleSerializer
32
+ end
33
+
34
+ class Fixnum
35
+ include SimpleSerializer
36
+ end
37
+
38
+ class Builder::XmlMarkup
39
+ def raw_tag!(tag, string)
40
+ _start_tag(tag, {})
41
+ _text string
42
+ _end_tag(tag)
43
+ end
44
+
45
+ def no_tag!(string)
46
+ _text string
47
+ end
48
+ end
49
+
50
+ # create a string class that doesn't get escaped when dumped into XML stream
51
+ class RawXML < String
52
+ def initialize(string, options={})
53
+ @options = options
54
+ super string
55
+ end
56
+
57
+ def to_xml(options_arg={})
58
+ options = @options.merge options_arg
59
+ builder = options[:builder] ||
60
+ Builder::XmlMarkup.new(:indent => options[:indent])
61
+
62
+ add_tag = options.has_key?(:no_tag) ? !options[:no_tag] : options[:root]
63
+ return builder.no_tag!(self.strip_xml_head) unless add_tag
64
+
65
+ tag = options[:root] || self.class.name.downcase
66
+ dasherize = ! options.has_key?(:dasherize) || options[:dasherize]
67
+ tag = dasherize ? tag.to_s.dasherize : tag
68
+
69
+ builder.raw_tag!(tag, self.strip_xml_head)
70
+ end
71
+ end
@@ -0,0 +1,63 @@
1
+ $: << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'test/unit'
3
+ require 'xml_serialization'
4
+
5
+ class TestXMLSerialization < Test::Unit::TestCase
6
+
7
+ def test_rawxml_should_pass_thru
8
+ rawxml = RawXML.new '<tag>content</tag>'
9
+ assert_equal rawxml, rawxml.to_xml
10
+ end
11
+
12
+ def test_fixnum
13
+ assert_equal '<fixnum>1</fixnum>', 1.to_xml
14
+ end
15
+
16
+ def test_string
17
+ assert_equal '<string>abc</string>', 'abc'.to_xml
18
+ end
19
+
20
+ def test_symbol
21
+ assert_equal '<symbol>abc</symbol>', :abc.to_xml
22
+ end
23
+
24
+ def test_array_of_strings
25
+ expected = <<EXPECTED
26
+ <?xml version="1.0" encoding="UTF-8"?>
27
+ <strings type="array">
28
+ <string>a</string>
29
+ <string>b</string>
30
+ <string>c</string>
31
+ </strings>
32
+ EXPECTED
33
+ assert_equal expected, ['a', 'b', 'c'].to_xml
34
+ end
35
+
36
+ def test_array_of_fixnums_with_root_tag_specified
37
+ expected = <<EXPECTED
38
+ <?xml version="1.0" encoding="UTF-8"?>
39
+ <numbers type="array">
40
+ <number>1</number>
41
+ <number>2</number>
42
+ <number>3</number>
43
+ </numbers>
44
+ EXPECTED
45
+ assert_equal expected, [1, 2, 3].to_xml(:root => 'numbers')
46
+ end
47
+
48
+ # this case is used by xslt_render to serialize the hash of controller instance variable values
49
+ def test_hash_with_array
50
+ expected = <<EXPECTED
51
+ <?xml version="1.0" encoding="UTF-8"?>
52
+ <hash>
53
+ <numbers type="array">
54
+ <number>1</number>
55
+ <number>2</number>
56
+ </numbers>
57
+ </hash>
58
+ EXPECTED
59
+ assert_equal expected, {'numbers' => [1, 2]}.to_xml
60
+ assert_equal expected, {'numbers' => ['1', '2']}.to_xml
61
+ end
62
+
63
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xml_serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Larry Baltz
9
+ - David Anderson
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2009-02-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: builder
17
+ requirement: &6695060 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *6695060
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &6693860 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *6693860
37
+ description: Extends the XML serialization support in activesupport to allow for arrays
38
+ containing strings, symbols, and integers.
39
+ email: david@folklogic.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - VERSION.yml
45
+ - lib/xml_serialization.rb
46
+ - test/xml_serialization_test.rb
47
+ homepage: http://github.com/alpinegizmo/xml_serialization
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --inline-source
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.15
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Extends the XML serialization support in activesupport to allow for arrays
73
+ containing strings, symbols, and integers.
74
+ test_files: []