lib/om/xml/dynamic_node.rb in om-1.6.1 vs lib/om/xml/dynamic_node.rb in om-1.7.0.rc1

- old
+ new

@@ -66,10 +66,11 @@ node = OM::XML::DynamicNode.new(name, index, @document, child, self) node.val=args end def val=(args) + @document.ng_xml_will_change! new_values = sanitize_new_values(args.first) 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 @@ -77,25 +78,23 @@ @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 end - if @document.respond_to?(:dirty=) - @document.dirty = true - end end def sanitize_new_values(new_values) # Sanitize new_values to always be a hash with indexes case new_values when Hash + new_values.each {|k, v| v = serialize(v) } when Array nv = new_values.dup new_values = {} - nv.each {|v| new_values[nv.index(v).to_s] = v} + nv.each {|v| new_values[nv.index(v).to_s] = serialize(v)} else - new_values = {"0"=>new_values} + new_values = {"0"=>serialize(new_values)} end new_values end def term_child_by_name(term, name) @@ -107,16 +106,47 @@ end def val query = xpath trim_text = !query.index("text()").nil? - return @document.find_by_xpath(query).collect {|node| (trim_text ? node.text.strip : node.text) } + val = @document.find_by_xpath(query).collect {|node| (trim_text ? node.text.strip : node.text) } + deserialize(val) end + + # @param string + # @return [String,Date,Integer] + def deserialize(val) + case term.type + when :date + val.map { |v| Date.parse(v)} + when :integer + val.map { |v| v.to_i} + else + val + end + end + + # @param val [String,Date,Integer] + def serialize (val) + case term.type + when :date + val.to_s + when :integer + val.to_s + else + val + end + + end def nodeset query = xpath trim_text = !query.index("text()").nil? return @document.find_by_xpath(query) + end + + def delete + nodeset.delete end def inspect val.inspect end