What Not to Do (a cautionary tale)



I wrote a little code generation system for my company. It's a small
script that reads in an XML file and an ERB template, feeds the XML file
to the template, and writes out the result to a file.

To help those in the company who don't know XPath, I extended REXML with
a couple convenience methods that let you search for elements by name
and/or attributes.

class REXML::Element
# Find all descendant nodes with a specified tag name and/or
attributes
def find( tag_name='*', attributes_to_match={} )
self.each_element(
".//#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
end

# Find all child nodes with a specified tag name and/or attributes
def kids( tag_name='*', attributes_to_match={} )
self.each_element(
"./#{REXML::Element.xpathfor(tag_name,attributes_to_match)}" ){}
end

...
end

This worked very nicely. Template writers could simply do something
like:
In this file I see the following classes: <%=
root.find( 'class' ).map{ |el| el.attributes[ 'name' ] }.join( ', '
)
%>

All was fine under 1.8.4.

I just upgraded to 1.8.5 and all templates using #find broke. After a
bit of sleuthing, it looks like REXML changed the Document#root method
in a way that caused it to call Elements#[], and that method uses
@element.find, mixed in from Enumerable. My Element#find was overriding
Enumerable#find, and all hell broke loose as a result.

Moral of the story (pick one or more):
[ ] Don't extend someone else's class with common method names
[ ] Don't upgrade your Ruby environment unless you need to



.



Relevant Pages

  • plugin architecture - repost
    ... I am building an application to help researcher record the outcomes of their ... These outcomes are document across a number of paper forms. ... I've created a number of these 'template' files. ... to use a specific template, I open the XML file, deserialize it, add data to ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Word 200 Macro using replace
    ... this field is populated from different fields in an XML file. ... The carraige ... returns aren't part of the template, they come from the XML file". ... > create a macro that will run automatically everytime a new .doc file is ...
    (microsoft.public.word.docmanagement)
  • Add or Modify Templates
    ... Office2007 RibbonXML for use in Office Template AddIns. ... General and XML File and a new blank XML file is created in the code window. ... named Ribbon XML Basic that creates a new XML as follows: ... I have found the XMLFile.zip folders in the Item Templates and Item ...
    (microsoft.public.vstudio.development)
  • "value substitution/population" in template XML using XPath
    ... I need to populate a large number of "template" XML files ... indicated in a seperate XML data file as, ... another XML file. ...
    (comp.text.xml)
  • Re: customising look and feel
    ... which renders incorrectly when applied to my original template. ... customise it. ... > office is currently running sps 2001 and I know that I will need to ... > Once I have all the web parts built I will simulate the upgrade again ...
    (microsoft.public.sharepoint.portalserver)