lib/happymapper.rb in nokogiri-happymapper-0.5.2 vs lib/happymapper.rb in nokogiri-happymapper-0.5.3
- old
+ new
@@ -119,14 +119,14 @@
#
# content :first_name, String
#
# @param [Symbol] name the name of the accessor that is created
# @param [String,Class] type the class name of the name of the class whcih
- # the object will be converted upon parsing
+ # the object will be converted upon parsing. By Default String class will be taken.
# @param [Hash] options additional parameters to send to the relationship
#
- def content(name, type, options={})
+ def content(name, type=String, options={})
@content = TextNode.new(name, type, options)
attr_accessor @content.method_name.intern
end
#
@@ -307,12 +307,16 @@
collection = []
nodes.each_slice(limit) do |slice|
- part = slice.map do |n|
- obj = new
+ part = slice.map do |n|
+
+ # If an existing HappyMapper object is provided, update it with the
+ # values from the xml being parsed. Otherwise, create a new object
+
+ obj = options[:update] ? options[:update] : new
attributes.each do |attr|
obj.send("#{attr.method_name}=",attr.from_xml_node(n, namespace, namespaces))
end
@@ -610,10 +614,18 @@
# then we assume that has been called recursively to generate a larger
# XML document.
write_out_to_xml ? builder.to_xml : builder
end
-
+
+ # Parse the xml and update this instance. This does not update instances
+ # of HappyMappers that are children of this object. New instances will be
+ # created for any HappyMapper children of this object.
+ #
+ # Params and return are the same as the class parse() method above.
+ def parse(xml, options = {})
+ self.class.parse(xml, options.merge!(:update => self))
+ end
end
require 'happymapper/item'
require 'happymapper/attribute'