apotonick/roar — pull request #28 by dlindahl (2012-05-14 01:55:21+00:00)
Title: Allow data attributes for link elements

This allows standard attributes such as `type` or custom data attributes such as `data-foo` without having to override `Hyperlink#params`.

I found overriding `Hyperlink#params`, as the documentation instructed, to be difficult to do at best, and impossible to do for data attributes because of the reliance on instance variables.

The crux of this Pull Request is that `Hyperlink` is now a subclass of `OpenStruct`. This allows one to set "properties" through an initial hash interface (i.e. via the `link` method) but retain a "getter method" interface and thus removing any internal instance variables.

The one gotcha I encountered was with parsing content from XML documents with Representable. While this Pull Request does work in that scenario, it doesn't allow custom attributes. It seems as if Representable's XML parsing requires properties to be set *before* the parsing is performed instead of the document defining what the attributes should be.

While this is might be a good thing as far as Representable is concerned, I'm not sure where this implementation lies in the context of Roar. I didn't want to make any changes to Representable in order to support this Pull Request, so I left that scenario alone. At worst, it works like it did before.

I offer this Pull Request as one possible solution for a feature that I needed. Specifically, I needed data attributes and there is just no way to get that to work with the current Hyperlink implementation. This change worked for me, but if it isn't good enough for Roar, then hopefully it will open up a discussion as to how best to implement this feature.