apotonick/roar — Issue #15: Render :links to JSON as hash instead of array
Author: KevinTriplett
Created: 2012-02-25 17:00:26+00:00
State: open

LOVING ROAR, thanks Nick! Only surprise has been consuming hypermedia links client-side, I have to know the array index of the link, discover the link by iterating over the array, or rendering the link as a property (faster but yucky).

We can access links as a hash server-side so why not client-side. Might make the LinkCollection class leaner if it were a Hash instead of an Array and didn't have to look up array elements? Then look up would be based on the rel value as the key? Here's some stoopid code, I have not tested it but I will if you think I'm in the correct location:

class LinkCollection < Hash
def [](rel)
self[rel] ? self[rel].href : nil
end

# Checks if the link is already contained by querying for its +rel+.
# If so, it gets replaced. Otherwise, the new link gets appended.
def update_link(link)
if self[link.rel]
# this assumes that the rel value is not changed
return self[link.rel] = link
end
self[link.rel] = link
end
end

I'm sure it's not that simple, please advise. Thanks again!