doxo-roxml 2.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/History.txt +279 -0
  2. data/MIT-LICENSE +18 -0
  3. data/Manifest.txt +103 -0
  4. data/README.rdoc +158 -0
  5. data/Rakefile +96 -0
  6. data/TODO +62 -0
  7. data/config/website.yml +2 -0
  8. data/examples/active_record.rb +70 -0
  9. data/examples/amazon.rb +33 -0
  10. data/examples/current_weather.rb +27 -0
  11. data/examples/dashed_elements.rb +20 -0
  12. data/examples/library.rb +40 -0
  13. data/examples/posts.rb +27 -0
  14. data/examples/twitter.rb +37 -0
  15. data/examples/xml/active_record.xml +70 -0
  16. data/examples/xml/amazon.xml +133 -0
  17. data/examples/xml/current_weather.xml +89 -0
  18. data/examples/xml/dashed_elements.xml +52 -0
  19. data/examples/xml/posts.xml +23 -0
  20. data/examples/xml/twitter.xml +422 -0
  21. data/lib/roxml.rb +511 -0
  22. data/lib/roxml/definition.rb +234 -0
  23. data/lib/roxml/extensions.rb +6 -0
  24. data/lib/roxml/extensions/array.rb +13 -0
  25. data/lib/roxml/extensions/array/conversions.rb +12 -0
  26. data/lib/roxml/extensions/deprecation.rb +33 -0
  27. data/lib/roxml/extensions/string.rb +6 -0
  28. data/lib/roxml/extensions/string/conversions.rb +5 -0
  29. data/lib/roxml/extensions/string/iterators.rb +12 -0
  30. data/lib/roxml/hash_definition.rb +25 -0
  31. data/lib/roxml/xml.rb +40 -0
  32. data/lib/roxml/xml/parsers/libxml.rb +86 -0
  33. data/lib/roxml/xml/parsers/rexml.rb +84 -0
  34. data/lib/roxml/xml/references.rb +299 -0
  35. data/roxml.gemspec +50 -0
  36. data/spec/definition_spec.rb +490 -0
  37. data/spec/examples/active_record_spec.rb +40 -0
  38. data/spec/examples/amazon_spec.rb +53 -0
  39. data/spec/examples/current_weather_spec.rb +37 -0
  40. data/spec/examples/dashed_elements_spec.rb +20 -0
  41. data/spec/examples/library_spec.rb +46 -0
  42. data/spec/examples/post_spec.rb +24 -0
  43. data/spec/examples/twitter_spec.rb +32 -0
  44. data/spec/roxml_spec.rb +372 -0
  45. data/spec/shared_specs.rb +15 -0
  46. data/spec/spec.opts +1 -0
  47. data/spec/spec_helper.rb +33 -0
  48. data/spec/xml/parser_spec.rb +47 -0
  49. data/tasks/rspec.rake +21 -0
  50. data/tasks/test.rake +42 -0
  51. data/test/bugs/rexml_bugs.rb +15 -0
  52. data/test/fixtures/book_malformed.xml +5 -0
  53. data/test/fixtures/book_pair.xml +8 -0
  54. data/test/fixtures/book_text_with_attribute.xml +5 -0
  55. data/test/fixtures/book_valid.xml +5 -0
  56. data/test/fixtures/book_with_authors.xml +7 -0
  57. data/test/fixtures/book_with_contributions.xml +9 -0
  58. data/test/fixtures/book_with_contributors.xml +7 -0
  59. data/test/fixtures/book_with_contributors_attrs.xml +7 -0
  60. data/test/fixtures/book_with_default_namespace.xml +9 -0
  61. data/test/fixtures/book_with_depth.xml +6 -0
  62. data/test/fixtures/book_with_octal_pages.xml +4 -0
  63. data/test/fixtures/book_with_publisher.xml +7 -0
  64. data/test/fixtures/book_with_wrapped_attr.xml +3 -0
  65. data/test/fixtures/dictionary_of_attr_name_clashes.xml +8 -0
  66. data/test/fixtures/dictionary_of_attrs.xml +6 -0
  67. data/test/fixtures/dictionary_of_guarded_names.xml +6 -0
  68. data/test/fixtures/dictionary_of_mixeds.xml +4 -0
  69. data/test/fixtures/dictionary_of_name_clashes.xml +10 -0
  70. data/test/fixtures/dictionary_of_names.xml +4 -0
  71. data/test/fixtures/dictionary_of_texts.xml +10 -0
  72. data/test/fixtures/library.xml +30 -0
  73. data/test/fixtures/library_uppercase.xml +30 -0
  74. data/test/fixtures/muffins.xml +3 -0
  75. data/test/fixtures/nameless_ageless_youth.xml +2 -0
  76. data/test/fixtures/node_with_attr_name_conflicts.xml +1 -0
  77. data/test/fixtures/node_with_name_conflicts.xml +4 -0
  78. data/test/fixtures/numerology.xml +4 -0
  79. data/test/fixtures/person.xml +1 -0
  80. data/test/fixtures/person_with_guarded_mothers.xml +13 -0
  81. data/test/fixtures/person_with_mothers.xml +10 -0
  82. data/test/mocks/dictionaries.rb +57 -0
  83. data/test/mocks/mocks.rb +279 -0
  84. data/test/test_helper.rb +45 -0
  85. data/test/unit/definition_test.rb +235 -0
  86. data/test/unit/deprecations_test.rb +24 -0
  87. data/test/unit/to_xml_test.rb +81 -0
  88. data/test/unit/xml_attribute_test.rb +39 -0
  89. data/test/unit/xml_block_test.rb +81 -0
  90. data/test/unit/xml_bool_test.rb +122 -0
  91. data/test/unit/xml_convention_test.rb +150 -0
  92. data/test/unit/xml_hash_test.rb +115 -0
  93. data/test/unit/xml_initialize_test.rb +49 -0
  94. data/test/unit/xml_name_test.rb +141 -0
  95. data/test/unit/xml_namespace_test.rb +76 -0
  96. data/test/unit/xml_object_test.rb +207 -0
  97. data/test/unit/xml_required_test.rb +93 -0
  98. data/test/unit/xml_text_test.rb +71 -0
  99. data/vendor/override_rake_task/README +30 -0
  100. data/vendor/override_rake_task/init.rb +1 -0
  101. data/vendor/override_rake_task/install.rb +46 -0
  102. data/vendor/override_rake_task/lib/override_rake_task.rb +16 -0
  103. data/website/index.html +98 -0
  104. metadata +234 -0
data/lib/roxml.rb ADDED
@@ -0,0 +1,511 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__)) unless
2
+ $LOAD_PATH.include?(File.dirname(__FILE__)) || $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'uri'
5
+
6
+ %w(extensions definition xml).each do |file|
7
+ require File.join('roxml', file)
8
+ end
9
+
10
+ module ROXML # :nodoc:
11
+ VERSION = '2.5.3'
12
+
13
+ def self.included(base) # :nodoc:
14
+ base.class_eval do
15
+ extend ClassMethods::Accessors,
16
+ ClassMethods::Declarations,
17
+ ClassMethods::Operations
18
+ include InstanceMethods
19
+ end
20
+ end
21
+
22
+ module InstanceMethods # :nodoc:
23
+ # Returns a LibXML::XML::Node or a REXML::Element representing this object
24
+ def to_xml(name = self.class.tag_name)
25
+ returning XML::Node.new(name.to_s) do |root|
26
+ self.class.roxml_attrs.each do |attr|
27
+ ref = attr.to_ref(self)
28
+ value = ref.to_xml
29
+ unless value.nil?
30
+ ref.update_xml(root, value)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # This class defines the annotation methods that are mixed into your
38
+ # Ruby classes for XML mapping information and behavior.
39
+ #
40
+ # See xml_name, xml_initialize, xml, xml_reader and xml_accessor for
41
+ # available annotations.
42
+ #
43
+ module ClassMethods # :nodoc:
44
+ module Declarations
45
+ # Sets the name of the XML element that represents this class. Use this
46
+ # to override the default lowercase class name.
47
+ #
48
+ # Example:
49
+ # class BookWithPublisher
50
+ # xml_name :book
51
+ # end
52
+ #
53
+ # Without the xml_name annotation, the XML mapped tag would have been "bookwithpublisher".
54
+ #
55
+ def xml_name(name)
56
+ @roxml_tag_name = name
57
+ end
58
+
59
+ # Sets the namemespace for attributes and elements of this class. You can override
60
+ # this value on individual elements via the :from option
61
+ #
62
+ # Example:
63
+ # class Book
64
+ # xml_namespace :aws
65
+ #
66
+ # xml_reader :default_namespace
67
+ # xml_reader :different_namespace, :from => 'different:namespace'
68
+ # xml_reader :no_namespace, :from => ':no_namespace'
69
+ # end
70
+ #
71
+ # <aws:book xmlns:aws="http://www.aws.com/aws" xmlns:different="http://www.aws.com/different">
72
+ # <aws:default_namespace>value</aws:default_namespace>
73
+ # <different:namespace>value</different:namespace>
74
+ # <no_namespace>value</no_namespace>
75
+ # </aws:book>
76
+ #
77
+ def xml_namespace(namespace)
78
+ @roxml_namespace = namespace.to_s
79
+ end
80
+
81
+ # Most xml documents have a consistent naming convention, for example, the node and
82
+ # and attribute names might appear in CamelCase. xml_convention enables you to adapt
83
+ # the roxml default names for this object to suit this convention. For example,
84
+ # if I had a document like so:
85
+ #
86
+ # <XmlDoc>
87
+ # <MyPreciousData />
88
+ # <MoreToSee InAttrs="" />
89
+ # </XmlDoc>
90
+ #
91
+ # Then I could access it's contents by defining the following class:
92
+ #
93
+ # class XmlDoc
94
+ # include ROXML
95
+ # xml_convention :camelcase
96
+ # xml_reader :my_precious_data
97
+ # xml_reader :in_attrs, :in => 'MoreToSee'
98
+ # end
99
+ #
100
+ # You may supply a block or any #to_proc-able object as the argument,
101
+ # and it will be called against the default node and attribute names before searching
102
+ # the document. Here are some example declaration:
103
+ #
104
+ # xml_convention :upcase
105
+ # xml_convention &:camelcase
106
+ # xml_convention {|val| val.gsub('_', '').downcase }
107
+ #
108
+ # See ActiveSupport::CoreExtensions::String::Inflections for more prepackaged formats
109
+ #
110
+ # Note that the xml_convention is also applied to the default root-level tag_name,
111
+ # but in this case an underscored version of the name is applied, for convenience.
112
+ def xml_convention(to_proc_able = nil, &block)
113
+ raise ArgumentError, "conventions are already set" if @roxml_naming_convention
114
+ @roxml_naming_convention =
115
+ if to_proc_able
116
+ raise ArgumentError, "only one conventions can be set" if block_given?
117
+ to_proc_able.to_proc
118
+ elsif block_given?
119
+ block
120
+ end
121
+ end
122
+
123
+ def roxml_naming_convention # :nodoc:
124
+ (@roxml_naming_convention || begin
125
+ superclass.roxml_naming_convention if superclass.respond_to?(:roxml_naming_convention)
126
+ end).freeze
127
+ end
128
+
129
+ # Declares a reference to a certain xml element, whether an attribute, a node,
130
+ # or a typed collection of nodes. This method does not add a corresponding accessor
131
+ # to the object. For that behavior see the similar methods: .xml_reader and .xml_accessor.
132
+ #
133
+ # == Sym Option
134
+ # [sym] Symbol representing the name of the accessor.
135
+ #
136
+ # === Default naming
137
+ # This name will be the default node or attribute name searched for,
138
+ # if no other is declared. For example,
139
+ #
140
+ # xml_reader :bob
141
+ # xml_accessor :pony, :from => :attr
142
+ #
143
+ # are equivalent to:
144
+ #
145
+ # xml_reader :bob, :from => 'bob'
146
+ # xml_accessor :pony, :from => '@pony'
147
+ #
148
+ # === Boolean attributes
149
+ # If the name ends in a ?, ROXML will attempt to coerce the value to true or false,
150
+ # with True, TRUE, true and 1 mapping to true and False, FALSE, false and 0 mapping
151
+ # to false, as shown below:
152
+ #
153
+ # xml_reader :desirable?
154
+ # xml_reader :bizzare?, :from => '@BIZZARE'
155
+ #
156
+ # x = #from_xml(%{
157
+ # <object BIZZARE="1">
158
+ # <desirable>False</desirable>
159
+ # </object>
160
+ # })
161
+ # x.desirable?
162
+ # => false
163
+ # x.bizzare?
164
+ # => true
165
+ #
166
+ # If an unexpected value is encountered, the attribute will be set to nil,
167
+ # unless you provide a block, in which case the block will recived
168
+ # the actual unexpected value.
169
+ #
170
+ # #from_xml(%{
171
+ # <object>
172
+ # <desirable>Dunno</desirable>
173
+ # </object>
174
+ # }).desirable?
175
+ # => nil
176
+ #
177
+ # xml_reader :strange? do |val|
178
+ # val.upcase
179
+ # end
180
+ #
181
+ # #from_xml(%{
182
+ # <object>
183
+ # <strange>Dunno</strange>
184
+ # </object>
185
+ # }).strange?
186
+ # => DUNNO
187
+ #
188
+ # == Blocks
189
+ # You may also pass a block which manipulates the associated parsed value.
190
+ #
191
+ # class Muffins
192
+ # include ROXML
193
+ #
194
+ # xml_reader(:count, :from => 'bakers_dozens') {|val| val.to_i * 13 }
195
+ # end
196
+ #
197
+ # For hash types, the block recieves the key and value as arguments, and they should
198
+ # be returned as an array of [key, value]
199
+ #
200
+ # For array types, the entire array is passed in, and must be returned in the same fashion.
201
+ #
202
+ # == Options
203
+ # === :as
204
+ # ==== Basic Types
205
+ # Allows you to specify one of several basic types to return the value as. For example
206
+ #
207
+ # xml_reader :count, :as => Integer
208
+ #
209
+ # is equivalent to:
210
+ #
211
+ # xml_reader(:count) {|val| Integer(val) unless val.empty? }
212
+ #
213
+ # Such block shorthands for Integer, Float, Fixnum, BigDecimal, Date, Time, and DateTime
214
+ # are currently available, but only for non-Hash declarations.
215
+ #
216
+ # To reference many elements, put the desired type in a literal array. e.g.:
217
+ #
218
+ # xml_reader :counts, :as => [Integer]
219
+ #
220
+ # Even an array of text nodes can be specified with :as => []
221
+ #
222
+ # xml_reader :quotes, :as => []
223
+ #
224
+ # === Other ROXML Class
225
+ # Declares an accessor that represents another ROXML class as child XML element
226
+ # (one-to-one or composition) or array of child elements (one-to-many or
227
+ # aggregation) of this type. Default is one-to-one. For one-to-many, simply pass the class
228
+ # as the only element in an array.
229
+ #
230
+ # Composition example:
231
+ # <book>
232
+ # <publisher>
233
+ # <name>Pragmatic Bookshelf</name>
234
+ # </publisher>
235
+ # </book>
236
+ #
237
+ # Can be mapped using the following code:
238
+ # class Book
239
+ # xml_reader :publisher, :as => Publisher
240
+ # end
241
+ #
242
+ # Aggregation example:
243
+ # <library>
244
+ # <books>
245
+ # <book/>
246
+ # <book/>
247
+ # </books>
248
+ # </library>
249
+ #
250
+ # Can be mapped using the following code:
251
+ # class Library
252
+ # xml_reader :books, :as => [Book], :in => "books"
253
+ # end
254
+ #
255
+ # If you don't have the <books> tag to wrap around the list of <book> tags:
256
+ # <library>
257
+ # <name>Ruby books</name>
258
+ # <book/>
259
+ # <book/>
260
+ # </library>
261
+ #
262
+ # You can skip the wrapper argument:
263
+ # xml_reader :books, :as => [Book]
264
+ #
265
+ # ==== Hash
266
+ # Somewhere between the simplicity of a :text/:attr mapping, and the complexity of
267
+ # a full Object/Type mapping, lies the Hash mapping. It serves in the case where you have
268
+ # a collection of key-value pairs represented in your xml. You create a hash declaration by
269
+ # passing a hash mapping as the type argument. A few examples:
270
+ #
271
+ # ===== Hash of element contents
272
+ # For xml such as this:
273
+ #
274
+ # <dictionary>
275
+ # <definition>
276
+ # <word/>
277
+ # <meaning/>
278
+ # </definition>
279
+ # <definition>
280
+ # <word/>
281
+ # <meaning/>
282
+ # </definition>
283
+ # </dictionary>
284
+ #
285
+ # You can individually declare your key and value names:
286
+ # xml_reader :definitions, :as => {:key => 'word',
287
+ # :value => 'meaning'}
288
+ #
289
+ # ===== Hash of :content &c.
290
+ # For xml such as this:
291
+ #
292
+ # <dictionary>
293
+ # <definition word="quaquaversally">adjective: (of a geological formation) sloping downward from the center in all directions.</definition>
294
+ # <definition word="tergiversate">To use evasions or ambiguities; equivocate.</definition>
295
+ # </dictionary>
296
+ #
297
+ # You can individually declare the key and value, but with the attr, you need to provide both the type
298
+ # and name of that type (i.e. {:attr => :word}), because omitting the type will result in ROXML
299
+ # defaulting to :text
300
+ # xml_reader :definitions, :as => {:key => {:attr => 'word'},
301
+ # :value => :content}
302
+ #
303
+ # ===== Hash of :name &c.
304
+ # For xml such as this:
305
+ #
306
+ # <dictionary>
307
+ # <quaquaversally>adjective: (of a geological formation) sloping downward from the center in all directions.</quaquaversally>
308
+ # <tergiversate>To use evasions or ambiguities; equivocate.</tergiversate>
309
+ # </dictionary>
310
+ #
311
+ # You can pick up the node names (e.g. quaquaversally) using the :name keyword:
312
+ # xml_reader :definitions, :as => {:key => :name,
313
+ # :value => :content}
314
+ #
315
+ # === :from
316
+ # The name by which the xml value will be found, either an attribute or tag name in XML.
317
+ # Default is sym, or the singular form of sym, in the case of arrays and hashes.
318
+ #
319
+ # This value may also include XPath notation.
320
+ #
321
+ # ==== :from => :content
322
+ # When :from is set to :content, this refers to the content of the current node,
323
+ # rather than a sub-node. It is equivalent to :from => '.'
324
+ #
325
+ # Example:
326
+ # class Contributor
327
+ # xml_reader :name, :from => :content
328
+ # xml_reader :role, :from => :attr
329
+ # end
330
+ #
331
+ # To map:
332
+ # <contributor role="editor">James Wick</contributor>
333
+ #
334
+ # ==== :from => :attr
335
+ # When :from is set to :attr, this refers to the content of an attribute,
336
+ # rather than a sub-node. It is equivalent to :from => '@attribute_name'
337
+ #
338
+ # Example:
339
+ # class Book
340
+ # xml_reader :isbn, :from => "@ISBN"
341
+ # xml_accessor :title, :from => :attr # :from defaults to '@title'
342
+ # end
343
+ #
344
+ # To map:
345
+ # <book ISBN="0974514055" title="Programming Ruby: the pragmatic programmers' guide" />
346
+ #
347
+ # ==== :from => :text
348
+ # The default source, if none is specified, this means the accessor
349
+ # represents a text node from XML. This is documented for completeness
350
+ # only. You should just leave this option off when you want the default behavior,
351
+ # as in the examples below.
352
+ #
353
+ # :text is equivalent to :from => accessor_name, and you should specify the
354
+ # actual node name if it differs, as in the case of :author below.
355
+ #
356
+ # Example:
357
+ # class Book
358
+ # xml_reader :author, :from => 'Author
359
+ # xml_accessor :description, :cdata => true
360
+ # xml_reader :title
361
+ # end
362
+ #
363
+ # To map:
364
+ # <book>
365
+ # <title>Programming Ruby: the pragmatic programmers' guide</title>
366
+ # <description><![CDATA[Probably the best Ruby book out there]]></description>
367
+ # <Author>David Thomas</Author>
368
+ # </book>
369
+ #
370
+ # Likewise, a number of :text node values can be collected in an array like so:
371
+ #
372
+ # Example:
373
+ # class Library
374
+ # xml_reader :books, :as => []
375
+ # end
376
+ #
377
+ # To map:
378
+ # <library>
379
+ # <book>To kill a mockingbird</book>
380
+ # <book>House of Leaves</book>
381
+ # <book>Gödel, Escher, Bach</book>
382
+ # </library>
383
+ #
384
+ # === Other Options
385
+ # [:in] An optional name of a wrapping tag for this XML accessor.
386
+ # This can include other xpath values, which will be joined with :from with a '/'
387
+ # [:else] Default value for attribute, if missing from the xml on .from_xml
388
+ # [:required] If true, throws RequiredElementMissing when the element isn't present
389
+ # [:frozen] If true, all results are frozen (using #freeze) at parse-time.
390
+ # [:cdata] true for values which should be input from or output as cdata elements
391
+ # [:to_xml] this proc is applied to the attributes value outputting the instance via #to_xml
392
+ #
393
+ def xml_attr(*syms, &block)
394
+ opts = syms.extract_options!
395
+ syms.map do |sym|
396
+ returning Definition.new(sym, opts, &block) do |attr|
397
+ if roxml_attrs.map(&:accessor).include? attr.accessor
398
+ raise "Accessor #{attr.accessor} is already defined as XML accessor in class #{self.name}"
399
+ end
400
+ @roxml_attrs << attr
401
+ end
402
+ end
403
+ end
404
+
405
+ # Declares a read-only xml reference. See xml_attr for details.
406
+ #
407
+ # Note that while xml_reader does not create a setter for this attribute,
408
+ # its value can be modified indirectly via methods. For more complete
409
+ # protection, consider the :frozen option.
410
+ def xml_reader(*syms, &block)
411
+ xml_attr(*syms, &block).each do |attr|
412
+ add_reader(attr)
413
+ end
414
+ end
415
+
416
+ # Declares a writable xml reference. See xml_attr for details.
417
+ #
418
+ # Note that while xml_accessor does create a setter for this attribute,
419
+ # you can use the :frozen option to prevent its value from being
420
+ # modified indirectly via methods.
421
+ def xml_accessor(*syms, &block)
422
+ xml_attr(*syms, &block).each do |attr|
423
+ add_reader(attr)
424
+ attr_writer(attr.attr_name)
425
+ end
426
+ end
427
+
428
+ private
429
+ def add_reader(attr)
430
+ define_method(attr.accessor) do
431
+ instance_variable_get(attr.instance_variable_name)
432
+ end
433
+ end
434
+ end
435
+
436
+ module Accessors
437
+ # Returns the tag name (also known as xml_name) of the class.
438
+ # If no tag name is set with xml_name method, returns default class name
439
+ # in lowercase.
440
+ #
441
+ # If xml_convention is set, it is called with an *underscored* version of
442
+ # the class name. This is because active support's inflector generally expects
443
+ # an underscored version, and several operations (e.g. camelcase(:lower), dasherize)
444
+ # do not work without one.
445
+ def tag_name
446
+ return roxml_tag_name if roxml_tag_name
447
+
448
+ if tag_name = name.split('::').last
449
+ roxml_naming_convention ? roxml_naming_convention.call(tag_name.underscore) : tag_name.downcase
450
+ end
451
+ end
452
+
453
+ def roxml_tag_name # :nodoc:
454
+ @roxml_tag_name || begin
455
+ superclass.roxml_tag_name if superclass.respond_to?(:roxml_tag_name)
456
+ end
457
+ end
458
+
459
+ def roxml_namespace # :nodoc:
460
+ @roxml_namespace || begin
461
+ superclass.roxml_namespace if superclass.respond_to?(:roxml_namespace)
462
+ end
463
+ end
464
+
465
+ # Returns array of internal reference objects, such as attributes
466
+ # and composed XML objects
467
+ def roxml_attrs
468
+ @roxml_attrs ||= []
469
+ (@roxml_attrs + (superclass.respond_to?(:roxml_attrs) ? superclass.roxml_attrs : [])).freeze
470
+ end
471
+ end
472
+
473
+ module Operations
474
+ #
475
+ # Creates a new Ruby object from XML using mapping information
476
+ # annotated in the class.
477
+ #
478
+ # The input data is either an XML::Node, String, Pathname, or File representing
479
+ # the XML document.
480
+ #
481
+ # Example
482
+ # book = Book.from_xml(File.read("book.xml"))
483
+ # or
484
+ # book = Book.from_xml("<book><name>Beyond Java</name></book>")
485
+ #
486
+ # _initialization_args_ passed into from_xml will be passed into
487
+ # the object's .new, prior to populating the xml_attrs.
488
+ #
489
+ # After the instatiation and xml population
490
+ #
491
+ # See also: xml_initialize
492
+ #
493
+ def from_xml(data, *initialization_args)
494
+ xml = XML::Node.from(data)
495
+
496
+ returning new(*initialization_args) do |inst|
497
+ roxml_attrs.each do |attr|
498
+ value = attr.to_ref(inst).value_in(xml)
499
+ inst.respond_to?(attr.setter) \
500
+ ? inst.send(attr.setter, value) \
501
+ : inst.instance_variable_set(attr.instance_variable_name, value)
502
+ end
503
+ inst.send(:after_parse) if inst.respond_to?(:after_parse, true)
504
+ end
505
+ rescue ArgumentError => e
506
+ raise e, e.message + " for class #{self}"
507
+ end
508
+ end
509
+ end
510
+ end
511
+