public
Description: Ruby WDDX gem.
Homepage: http://code.juretta.com/ruby/wddx/
Clone URL: git://github.com/juretta/wddx.git
Search Repo:
Initial import from latest stabel SVN branch
juretta (author)
Fri Feb 29 21:21:27 -0800 2008
commit  e3bd2e6d586f44c07740b981351512daefdf18f6
tree    8b602af320a77753b0096000d6a99064b2846358
...
 
 
 
 
0
...
1
2
3
4
5
0
@@ -0,0 +1,4 @@
0
+*.tmproj
0
+\.DS_Store
0
+TODO
0
+_support
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
0
@@ -0,0 +1,24 @@
0
+== Release 0.3.2
0
+* Bugfix release: Fixes bug 13288.
0
+
0
+== Release 0.3.1
0
+* Bugfix release: Fixes bugs 10148 and 10149 reported by Rhett Sutphin
0
+* Patch [#10152] kindly provided by Rhett Sutphin!
0
+
0
+== Release 0.3.0
0
+* New example wddx_serialize.rb added (see examples/ subdirectory).
0
+* WDDX::Struct now includes Enumerable
0
+* Documentation improved
0
+* Symbol now responds_to?(:to_wddx)
0
+
0
+== Release 0.2.1
0
+Bugfix Release - 2006-12-20
0
+* Fixed TcWddxComplexTest
0
+
0
+== Release 0.2.0
0
+Added Rails support - 2006-12-19
0
+
0
+Rails ActiveRecord::Base objects can be serialised to WDDX-XML using the +to_wddx+ method.
0
+
0
+== Release 0.1.0
0
+Initial release - 2006-12-18
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
0
@@ -0,0 +1,25 @@
0
+History.txt
0
+README.txt
0
+lib/wddx.rb
0
+lib/wddx/version.rb
0
+lib/wddx/core_ext.rb
0
+lib/wddx/deserializer.rb
0
+lib/wddx/rails_plugin.rb
0
+lib/wddx/record_set.rb
0
+lib/wddx/serializer.rb
0
+lib/wddx/struct.rb
0
+lib/wddx/wddx_packet.rb
0
+test/test_helper.rb
0
+test/wddx_test.rb
0
+test/fixtures/doc.1.xml
0
+test/fixtures/invalid_wddx.1.xml
0
+test/fixtures/java.test.xml
0
+test/fixtures/php_test.xml
0
+test/fixtures/simple_array.2.xml
0
+test/fixtures/simple_array.xml
0
+test/fixtures/test.1.xml
0
+test/fixtures/ascii_characters.txt
0
+test/fixtures/wddx_dtd_10.dtd
0
+examples/wddx_deserialize.rb
0
+examples/wddx_serialize.rb
0
+examples/example_helper.rb
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
0
@@ -0,0 +1,171 @@
0
+== WDDX
0
+
0
+From http://www.openwddx.org/faq/:
0
+ WDDX is an XML-based technology that enables the exchange of complex data between Web programming languages,
0
+ creating what some refer to as 'Web syndicate networks'.
0
+ WDDX consists of a language-independent representation of data based on an XML 1.0 DTD, and a set of
0
+ modules for a wide variety of languages that use WDDX.
0
+ WDDX can be used with HTTP, SMTP, POP, FTP and other Internet protocols that support transferring textual data.
0
+
0
+WDDX Home: http://www.openwddx.org/
0
+
0
+The Ruby WDDX Gem enables easy usage of WDDX in Ruby.
0
+
0
+== Author
0
+
0
+Copyright (C) 2005, 2006, 2007 by Stefan Saasen <s-NOSPAM-REMOVE@juretta.com> - http://juretta.com/
0
+
0
+== Installation
0
+
0
+Just run <tt>[sudo] gem install wddx</tt> to install the WDDX Gem.
0
+
0
+== Deserialization
0
+You can very easily deserialize WDDX data from a file or from a string.
0
+
0
+Simple usage example:
0
+
0
+ require 'rubygems'
0
+ require 'wddx'
0
+ # data.xml contains <?xml version="1.0">\
0
+ # <wddxPacket version='1.0'><header><comment>A comment</comment></header><data><string>Klaus Tester</string></data></wddxPacket>
0
+ wddx = WDDX.load(File.open("data.xml"))
0
+ puts wddx.comment # "A comment"
0
+ puts wddx.data.class # String
0
+ puts wddx.data # "Klaus Tester"
0
+
0
+WDDX.load accepts a string with xml content, a file object or any object that provides a +read+ method.
0
+ packet = WDDX.load(open("http://wddx.rubyforge.org/wddx.xml"))
0
+or
0
+ packet = WDDX.load(File.open("data.xml"))
0
+or
0
+ packet = WDDX.load(open("data.xml"))
0
+or
0
+ xml = "<wddxPacket version='1.0'><header/><data><number>123.667</number></data></wddxPacket>"
0
+ packet = WDDX.load(xml)
0
+ puts w.data # => 123.667
0
+
0
+ xml = <<-EOF
0
+ <wddxPacket version='1.0'>
0
+ <header/>
0
+ <data>
0
+ <struct>
0
+ <var name='aNull'>
0
+ <null/>
0
+ </var>
0
+ <var name='aString'>
0
+ <string>a string</string>
0
+ </var>
0
+ </struct>
0
+ </data>
0
+ </wddxPacket>
0
+ EOF
0
+ w = WDDX.load(xml)
0
+ p w.data # => {"aString"=>"a string", "aNull"=>nil}
0
+
0
+If the root element is a +struct+ you can use a shortcut version:
0
+
0
+ w.data["aString"] # => "a string"
0
+
0
+can be written as:
0
+
0
+ w.aString # => "a string"
0
+
0
+
0
+== Serialization
0
+The core Ruby Classes +Symbol+, +String+, +Numeric+, +true+, +false+, +nil+, +Hash+, +Array+ and +Time+ can be serialized by calling
0
++to_wddx+.
0
+
0
+ require 'wddx'
0
+ "Stefan Saasen".to_wddx # => "<wddxPacket version='1.0'><header/><data>\
0
+ # <string>Stefan Saasen</string></data></wddxPacket>"
0
+
0
+ 123.667.to_wddx # => "<wddxPacket version='1.0'><header/><data>\
0
+ # <number>123.667</number></data></wddxPacket>"
0
+
0
+ Math::PI.to_wddx # => "<wddxPacket version='1.0'><header/><data>\
0
+ # <number>3.14159265358979</number></data></wddxPacket>"
0
+
0
+ [1000, "Klaus Tester"].to_wddx # => "<wddxPacket version='1.0'><header/><data>\
0
+ # <array length='2'><number>1000</number>\
0
+ # <string>Klaus Tester</string></array></data></wddxPacket>"
0
+
0
+=== WDDX.dump
0
+You can use WDDX.dump to serialize Ruby objects.
0
+ WDDX.dump("Hallo Welt") # => "<wddxPacket version='1.0'><header/><data><string>Hallo Welt</string></data></wddxPacket>"
0
+
0
+
0
+Custom Ruby classes can be serialized by adding a +to_wddx_properties+ method (in the style of the YAML library) and calling WDDX.dump.
0
+
0
+ class SerializeFromOutside
0
+ def initialize(a,b,c)
0
+ @a, @b, @c = a, b, c
0
+ end
0
+
0
+ def to_wddx_properties
0
+ ["@a", "@c"]
0
+ end
0
+ end
0
+ obj = SerializeFromOutside.new("This is a", "This is b", 123)
0
+ WDDX.dump(obj) # => "<wddxPacket version='1.0'><header/><data><struct><var name='a'>\
0
+ # <string>This is a</string></var><var name='c'><number>123</number>\
0
+ # </var></struct></data></wddxPacket>"
0
+
0
+It is possible to include the WDDX module to get a behaviour similiar to the core Ruby classes.
0
+
0
+ class MyObject
0
+ include WDDX
0
+ attr_accessor :name, :value, :price
0
+
0
+ def to_wddx_properties
0
+ ["@name", :custom_price]
0
+ end
0
+
0
+ def custom_price
0
+ @price * 1.05
0
+ end
0
+ end
0
+
0
+ obj = MyObject.new("Stefan Saasen", 120)
0
+ puts ob.to_wddx
0
+ # => "<wddxPacket version='1.0'><header/><data><struct><var name='name'>\
0
+ # <string>Stefan Saasen</string></var><var name='custom_price'><number>126.0</number>\
0
+ # </var></struct></data></wddxPacket>"
0
+
0
+== WDDX Data types
0
+
0
+WDDX defines some data types which can not be mapped to native ruby classes. WDDX::Binary represents a binary object (which is in fact a BASE64 encoded String). WDDX::RecordSet represents a RecordSet with data rows an column meta information.
0
+
0
+== Type mapping
0
+The following table shows the data type mapping WDDX <=> Ruby.
0
+ WDDX Type Ruby Type
0
+ --------- ---------------------
0
+ String String
0
+ Number Numeric
0
+ Boolean TrueClass, FalseClass
0
+ Datetime Time
0
+ Null nil
0
+ Binary WDDX::Binary
0
+ Array Array
0
+ Struct Hash
0
+ Recordset WDDX::RecordSet
0
+
0
+
0
+== Rails Plugin
0
+
0
+You can add the wddx gem to your rails application to be able to serialize ActiveRecord objects to WDDX.
0
+
0
+In config/environment.rb add:
0
+ require 'wddx'
0
+
0
+Example:
0
+
0
+ ...
0
+ a_ar_obj.to_wddx # => <wddxPacket...
0
+
0
+
0
+== See
0
+
0
+Homepage:: http://rubyforge.org/projects/wddx/
0
+Blog:: http://juretta.com/
0
+Bugtracker:: http://rubyforge.org/tracker/?group_id=2715
0
+WDDX:: http://www.openwddx.org/
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
0
@@ -0,0 +1,60 @@
0
+# Author: Stefan Saasen <s@juretta.com>
0
+
0
+require 'rubygems'
0
+require 'rake'
0
+require 'rake/clean'
0
+require 'rcov/rcovtask'
0
+require 'hoe'
0
+require 'fileutils'
0
+
0
+include FileUtils
0
+require File.join(File.dirname(__FILE__), 'lib', 'wddx', 'version')
0
+
0
+RELEASE_TYPES = %w( gem ) # can use: gem, tar, zip
0
+REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
0
+VERS = ENV['VERSION'] || (Wddx::VERSION::STRING + (REV ? ".#{REV}" : ""))
0
+CLEAN.include ['**/.*.sw?', '*.gem', '.config']
0
+RDOC_OPTS = [
0
+ '--quiet',
0
+ '--title', "Ruby::WDDX documentation",
0
+ "--opname", "index.html",
0
+ "--line-numbers",
0
+ "--include", "lib/**/*.rb",
0
+ "--include", "examples/**/*.rb",
0
+ "--main", "README",
0
+ "--inline-source"
0
+]
0
+GEM_NAME = "wddx"
0
+DESCRIPTION = "Ruby API for the WDDX XML interchange format (see http://www.openwddx.org/)"
0
+
0
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
0
+hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
+ p.name = GEM_NAME
0
+ p.author = "Stefan Saasen"
0
+ p.description = DESCRIPTION
0
+ p.email = "s@juretta.com"
0
+ p.summary = DESCRIPTION
0
+ p.url = "http://#{GEM_NAME}.rubyforge.org"
0
+ p.lib_files = Dir["lib/**/*.rb"]
0
+ p.rubyforge_name = GEM_NAME
0
+ p.test_globs = ["test/**/tc_*.rb"]
0
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
0
+ p.clean_globs = CLEAN # An array of file patterns to delete on clean.
0
+ p.remote_rdoc_dir = '' # Release to root
0
+ p.spec_extras = {:dependencies => []} # - A hash of extra values to set in the gemspec.
0
+end
0
+
0
+Rcov::RcovTask.new("rcov") do |t|
0
+ t.test_files = FileList['test/*_test.rb']
0
+ t.verbose = false
0
+ t.rcov_opts << "--test-unit-only"
0
+ t.ruby_opts << "-Ilib:ext/rcovrt" # in order to use this rcov
0
+ t.output_dir = "coverage"
0
+end
0
+
0
+desc "Run basic unit tests"
0
+Rake::TestTask.new("test_units") { |t|
0
+ t.pattern = 'test/tc_*.rb'
0
+ t.verbose = false
0
+ t.warning = true
0
+}
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -0,0 +1,27 @@
0
+# $Id: example_helper.rb 109 2007-04-07 15:29:03Z stefan $
0
+# Author: Stefan Saasen <s@juretta.com>
0
+
0
+# only required to pretty print the XML examples
0
+require 'rexml/document'
0
+
0
+# Print XML example
0
+def print_xml(xml)
0
+ # Print method call
0
+ f = caller.first.split(":")
0
+ puts IO.readlines(f.first)[f.last.to_i-1].gsub(/(print_xml|\n|#(.*)$)/, "").strip + ": "
0
+ # Print XML
0
+ REXML::Document.new(xml).write($stdout, 2)
0
+ # Print separator
0
+ puts "\n" + ("~" * 60) + "\n"
0
+end
0
+
0
+# Print example
0
+def print_e(var)
0
+ # Print method call
0
+ f = caller.first.split(":")
0
+ print IO.readlines(f.first)[f.last.to_i-1].gsub(/(print_e|\n|#(.*)$)/, "").strip + ": "
0
+ # Print var
0
+ puts var
0
+ # Print separator
0
+ puts ("~" * 60) + "\n"
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
0
@@ -0,0 +1,110 @@
0
+# $Id: wddx_deserialize.rb 109 2007-04-07 15:29:03Z stefan $
0
+# Author: Stefan Saasen <s@juretta.com>
0
+
0
+require 'rubygems' # unless defined otherwise
0
+require 'wddx'
0
+
0
+=begin
0
+To deserialize a WDDX-Packet (XML) you need to use the WDDX.load method.
0
+WDDX.load accepts objects that respond to a :read method.
0
+Examples are String, File or OpenUri objects.
0
+=end
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+require 'example_helper'
0
+require 'open-uri'
0
+require 'pp'
0
+
0
+# Use open-uri to open a remote file
0
+begin
0
+ packet = WDDX.load(open("http://wddx.rubyforge.org/wddx.xml"))
0
+ puts packet.class
0
+ puts packet.comment # "A comment"
0
+ pp packet.data # serialized data
0
+
0
+ puts "-- " * 24
0
+
0
+ puts packet.data["aNumber"]
0
+ puts packet.aNumber
0
+rescue => e
0
+ $stderr.puts e.message
0
+end
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+
0
+xml =<<EOX
0
+<wddxPacket version='1.0'>
0
+ <header/>
0
+ <data>
0
+ <array length='9'>
0
+ <string>a string</string>
0
+ <null/>
0
+ <boolean value='true'/>
0
+ <boolean value='false'/>
0
+ <string>a_symbol</string>
0
+ <array length='2'>
0
+ <number>1</number>
0
+ <number>2</number>
0
+ </array>
0
+ <struct>
0
+ <var name='a'>
0
+ <string>b</string>
0
+ </var>
0
+ </struct>
0
+ <dateTime>2007-04-01T12:19:34+02:00</dateTime>
0
+ <number>3.4</number>
0
+ </array>
0
+ </data>
0
+ </wddxPacket>
0
+EOX
0
+
0
+wddx = WDDX.load xml
0
+# wddx.data contains the wddx data (in this case an array)
0
+print_e wddx.data.class # => Array
0
+print_e wddx.data[0] # => "a string"
0
+print_e wddx.data[0].class # => String
0
+
0
+print_e wddx.data[6].class # => Hash
0
+print_e wddx.data[6]["a"] # => "b"
0
+
0
+print_e wddx.data[7].class # => Time
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+xml =<<EOX
0
+<wddxPacket version='1.0'>
0
+ <header/>
0
+ <data>
0
+ <struct>
0
+ <var name='country'>
0
+ <string>New Zealand</string>
0
+ </var>
0
+ <var name='population'>
0
+ <number>4000000</number>
0
+ </var>
0
+ </struct>
0
+ </data>
0
+ </wddxPacket>
0
+EOX
0
+
0
+wddx = WDDX.load xml
0
+print_e wddx.data.class # => Hash
0
+
0
+print_e wddx.data["country"] # => "New Zealand"
0
+# To access a hash value you can use the key as method name on the wddx object.
0
+print_e wddx.country # => "New Zealand"
0
+print_e wddx.population # => 4000000
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+
0
+# Deserialize the xml that comes with this file
0
+# wddx.data contains the single string value "Klaus Tester"
0
+wddx = WDDX.load(DATA) # => DATA supports a :read method and can be used directly.
0
+print_e wddx.data # => Klaus Tester
0
+print_e wddx.comment # => "A comment"
0
+__END__
0
+<wddxPacket version='1.0'>
0
+ <header>
0
+ <comment>A comment</comment>
0
+ </header>
0
+ <data><string>Klaus Tester</string></data>
0
+</wddxPacket>
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
0
@@ -0,0 +1,114 @@
0
+# $Id: wddx_serialize.rb 109 2007-04-07 15:29:03Z stefan $
0
+# Author: Stefan Saasen <s@juretta.com>
0
+
0
+require 'rubygems'
0
+require 'wddx'
0
+
0
+# helper methods
0
+require 'example_helper'
0
+
0
+=begin
0
+To serialize Ruby objects different approaches exist.
0
+1. You can use WDDX.dump with the ruby object as argument
0
+2. The core classes (String, Numeric, Array, Hash...) have
0
+ a "to_wddx" method that can be called directly
0
+3. You can mixin the WDDX module into arbitrary classes to get a to_wddx method in your
0
+ custom classes
0
+=end
0
+
0
+# Use WDDX.dump to dump core Ruby objects
0
+print_xml WDDX.dump("klaus tester") # => <wddxPacket version='1.0'>...
0
+
0
+print_xml WDDX.dump([:sym, 123, Time.now])
0
+
0
+print_xml WDDX.dump({:key => 123})
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+
0
+# To serialize custom Ruby objects there is only one step necessary.
0
+# You need to define a "to_wddx_properties" method
0
+# which returns an array of properties or methods that should be serialized.
0
+# Then you can use WDDX.dump to serialize an instance of your class.
0
+class Class1
0
+ def initialize(a,b,c); @a, @b, @c = a, b, c; end
0
+ def custom_method; "a: #{@a}, b: #{@b}, c: #{@c}"; end
0
+
0
+ # serialize the instance variables @a and @c and the method custom_method
0
+ def to_wddx_properties; ["@a", "@c", :custom_method]; end
0
+end
0
+
0
+print_xml WDDX.dump(Class1.new(:a, 1, Time.now))
0
+
0
+# You can mixin the WDDX module which defines a default "to_wddx_properties" method
0
+# which serializes all instance variables.
0
+class Class2
0
+ include WDDX # defines a default to_wddx_properties method
0
+ def initialize(a,b,c)
0
+ @a, @b, @c = a, b, c
0
+ end
0
+end
0
+# WDDX.dump needs to_wddx_properties to serialize the custom Ruby object.
0
+print_xml WDDX.dump(Class2.new(:class2, 2, Time.now))
0
+
0
+# Moreover the WDDX module defines a "to_wddx" method which can be used to
0
+# serialize the object without using WDDX.dump.
0
+print_xml Class2.new(:class2, 3, Time.now).to_wddx
0
+
0
+# The WDDX module can be mixed into existing classes as well.
0
+require 'ostruct'
0
+# To dump every OpenStruct object you would certainly mixin the WDDX modul into
0
+# the OpenStruct class
0
+class OpenStruct
0
+ include WDDX
0
+ # serialize the virtual ostruct properties
0
+ def to_wddx_properties
0
+ @table.keys
0
+ end
0
+end
0
+
0
+country_de = OpenStruct.new({ "country" => "Germany", :population => 80_000_000 })
0
+print_xml WDDX.dump(country_de)
0
+# OR because of the mixed in wddx module
0
+print_xml country_de.to_wddx
0
+
0
+# change the properties that should be serialized using a singleton method
0
+def country_de.to_wddx_properties
0
+ [:country]
0
+end
0
+print_xml country_de.to_wddx # => only country will be serialized due to to_wddx_properties
0
+
0
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
+# Core ruby classes can be serialized by calling "to_wddx" directly.
0
+
0
+# String
0
+print_xml "ruby".to_wddx
0
+
0
+# Symbol
0
+print_xml :a_symbol.to_wddx
0
+
0
+# true
0
+print_xml true.to_wddx
0
+
0
+# false
0
+print_xml false.to_wddx
0
+
0
+# Numeric
0
+print_xml 12.to_wddx
0
+
0
+# Array
0
+print_xml ["a string", nil, true, false, :a_symbol, [1,2], {:a => "b"}, Time.now, 3.4].to_wddx
0
+
0
+# Hash
0
+print_xml({:a => 2}.to_wddx)
0
+
0
+# Time
0
+print_xml Time.now.to_wddx
0
+
0
+# Nil
0
+print_xml nil.to_wddx
0
+
0
+# to_wddx and WDDX.dump yield the same result
0
+["a string", nil, true, false, :a_symbol, [1,2], {:a => "b"}, Time.now, 3.4].each do |var|
0
+ print "#{var.class} Same? -> "
0
+ puts WDDX.dump(var).eql?(var.to_wddx)
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
0
@@ -0,0 +1,169 @@
0
+#--
0
+# $Id: wddx.rb 115 2007-08-22 21:11:31Z stefan $
0
+#
0
+# Copyright (C) 2005, 2006 by Stefan Saasen <s@juretta.com>
0
+#
0
+# Permission is hereby granted, free of charge, to any person obtaining
0
+# a copy of this software and associated documentation files (the
0
+# "Software"), to deal in the Software without restriction, including
0
+# without limitation the rights to use, copy, modify, merge, publish,
0
+# distribute, sublicense, and/or sell copies of the Software, and to
0
+# permit persons to whom the Software is furnished to do so, subject to
0
+# the following conditions:
0
+#
0
+# The above copyright notice and this permission notice shall be
0
+# included in all copies or substantial portions of the Software.
0
+#
0
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0
+#++
0
+require 'open-uri'
0
+#$:.unshift(File.dirname(__FILE__) + "/wddx") unless $:.include?(File.dirname(__FILE__) + "/wddx") || $:.include?(File.expand_path(File.dirname(__FILE__) + "/wddx"))
0
+
0
+Dir[File.join(File.dirname(__FILE__), 'wddx/**/*.rb')].sort.each { |lib| require lib }
0
+
0
+# == Usage
0
+#:include:README.txt
0
+module WDDX
0
+ # class methods
0
+ class << self
0
+ # Reads WDDX-XML from obj. Obj can be a String or every Object that responds to a "read" method (File, open-uri...)
0
+ # require 'open-uri'
0
+ # packet = WDDX.load(open("http://wddx.rubyforge.org/wddx.xml"))
0
+ def open(obj, &block)
0
+ if obj.respond_to?(:read)
0
+ return deserialize(obj.read)
0
+ end
0
+ return deserialize(obj)
0
+ end
0
+ alias :load :open
0
+
0
+ # Reads WDDX-XML from the file +filename+.
0
+ def from_file(filename, &block)
0
+ deserialize(open(filename))
0
+ end
0
+ alias :file :from_file
0
+
0
+ # Deserialize WDDX-XML to a WDDX::WddxPacket
0
+ # p = WDDX.deserialize(xml_string)
0
+ # p.data # => String, Number, Time...
0
+ def deserialize(xml_string)
0
+ Deserializer::Deserializer.new(xml_string).wddx_packet
0
+ end
0
+
0
+ # The method dump tries to serialize arbitrary Ruby objects.
0
+ # print WDDX.dump("Hello world") # => "<wddxPacket version='1.0'><header/><data><string>Hello world</string></data></wddxPacket>"
0
+ #
0
+ # You can add a +to_wddx_properties+ method to your class that returns an array of fields an method names (as symbol) that will be
0
+ # serialized as WDDX XML.
0
+ #
0
+ # class MyObj
0
+ # def initilize(name, price)
0
+ # @name, @price = name, price
0
+ # end
0
+ # def custom_price; @price * 1.05; end
0
+ #
0
+ # def to_wddx_properties
0
+ # ["@name", :custom_price]
0
+ # end
0
+ # end
0
+ #
0
+ # WDDX.dump(MyObj.new("Klaus Tester", 123))
0
+ def dump(var)
0
+ s = WDDX::Serializer::Serializer.new
0
+ s.add_var var
0
+ s.to_xml
0
+ end
0
+
0
+ end # class << self
0
+
0
+ # The WDDX module can be mixed into arbitrary classes.
0
+ # The method +wddx_serialize+ serializes the instance variables
0
+ # or the instance variables and methods output defined in +to_wddx_properties+
0
+ def wddx_serialize #:nodoc:
0
+ hash = {}
0
+ if self.respond_to?(:to_wddx_properties)
0
+ self.send(:to_wddx_properties).each do |prop|
0
+ if prop.respond_to?(:id2name)
0
+ hash[prop.id2name] = self.send(prop)
0
+ elsif prop =~ /^@[^@]/
0
+ hash[prop.gsub(/@/, "")] = instance_variable_get(prop)
0
+ end
0
+ end
0
+ else
0
+ self.instance_variables.collect {|var| hash[var.gsub(/@/, "")] = instance_variable_get(var)}
0
+ end
0
+ hash.wddx_serialize
0
+ end
0
+
0
+ # The WDDX module can be mixed into arbitrary classes.
0
+ # You can call +to_wddx+ to get a WDDX-XML representation of your object.
0
+ #
0
+ # class MyTest
0
+ # include WDDX
0
+ # def initialize(a,b)
0
+ # @a, @b = a, b
0
+ # end
0
+ # end
0
+ #
0
+ # t = MyTest.new("Hello world", Time.now)
0
+ # print t.to_wddx # => "<wddxPacket ..."
0
+ def to_wddx
0
+ s = WDDX::Serializer::Serializer.new
0
+ s.add_var self
0
+ s.to_xml
0
+ end
0
+
0
+ # = Binary
0
+ # The