lib/rubyfocus/document.rb in rubyfocus-0.5.9 vs lib/rubyfocus/document.rb in rubyfocus-0.5.11

- old
+ new

@@ -68,10 +68,11 @@ #--------------------------------------- # Use the linked fetcher to update the document def update if fetcher + raise RuntimeError, "Rubyfocus cannot currently read encrypted databases." if fetcher.encrypted? fetcher.update_full(self) else raise RuntimeError, "Tried to update a document with no fetcher." end end @@ -135,26 +136,37 @@ end end # Update an element in-place by applying xml. This method also takes into account: # * new nodes (i.e. silently creates if required) - # * tasks upgraded to projects - # * projects downgraded to tasks + # * tasks upgraded to projects (if task has a non-empty <project> element) + # * projects downgraded to tasks (if project has an empty <project> element) # Note that unlike add_element, this takes pure XML def update_element(node) element = self[node["id"]] # Does element already exist? if element # Quick check: is it a task being upgraded to a project? - if element.class == Rubyfocus::Task && Rubyfocus::Project.matches_node?(node) + # Upgrade criteria: non-empty project tag + if( + element.class == Rubyfocus::Task && + (node / "project *").size > 0 + ) + # Upgrade new_node = element.to_project new_node.apply_xml(node) add_element(new_node, overwrite:true) # or is the project being downgraded to a task? - elsif element.class == Rubyfocus::Project && !Rubyfocus::Project.matches_node?(node) + # Downgrade criteria: presence of an empty project tag + elsif( + element.class == Rubyfocus::Project && + (node / "project").size > 0 && + (node / "project *").size == 0 + ) + # Downgrade new_node = element.to_task new_node.apply_xml(node) add_element(new_node, overwrite:true) else @@ -163,9 +175,24 @@ end else # Create a new node and add it Rubyfocus::Parser.parse(self,node) end + end + + # Update an element in-place by creating a new element, deleting the old, and adding the new. + # This method is chiefly used for patching OF documents using V1 patches. Properties not explicitly + # mentioned in the patch are reverted to their default values. + # This method also takes into account: + # * new nodes (i.e. silently creates if required) + # * tasks upgraded to projects (if task has a <project> element) + # * projects downgraded to tasks (if project has no <project> element) + # Note that unlike add_element, this takes pure XML + def overwrite_element(node) + element = self[node["id"]] + self.remove_element(element) if element + + Rubyfocus::Parser.parse(self, node) end #------------------------------------------------------------------------------- # Searchable stuff def elements \ No newline at end of file