lib/om/xml/dynamic_node.rb in om-2.2.1 vs lib/om/xml/dynamic_node.rb in om-3.0.0.beta1
- old
+ new
@@ -67,14 +67,22 @@
node.val=args
end
def val=(args)
@document.ng_xml_will_change!
+ raise ArgumentError, "The new_values passed to #{to_pointer} must be an array or a string. You provided #{args.first.inspect}" unless [Array, String, NilClass].include?(args.first.class)
new_values = term.sanitize_new_values(args.first)
+ existing_nodes = @document.find_by_xpath(xpath)
+ if existing_nodes.length > new_values.length
+ starting_index = new_values.length + 1
+ starting_index.upto(existing_nodes.size).each do |index|
+ @document.term_value_delete select: xpath, child_index: index
+ end
+ end
new_values.keys.sort { |a,b| a.to_i <=> b.to_i }.each do |y|
z = new_values[y]
## If we pass something that already has an index on it, we should be able to add it.
- if @document.find_by_xpath(xpath)[y.to_i].nil? || y.to_i == -1
+ if existing_nodes[y.to_i].nil? || y.to_i == -1
parent_pointer = parent ? parent.to_pointer : nil
@document.term_values_append(:parent_select=> parent_pointer,:parent_index=>0,:template=>to_pointer,:values=>z)
else
@document.term_value_update(xpath, y.to_i, z)
end