alpinegizmo-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: 0
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,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alpinegizmo-xml_serialization
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Larry Baltz
8
+ - David Anderson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-02-22 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: builder
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
36
+ description: Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.
37
+ email: david@folklogic.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - VERSION.yml
46
+ - lib/xml_serialization.rb
47
+ - test/xml_serialization_test.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/alpinegizmo/xml_serialization
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --inline-source
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Extends the XML serialization support in activesupport to allow for arrays containing strings, symbols, and integers.
75
+ test_files: []
76
+