{{{
Just a quick round-up for new things in the “Roar”:https://github.com/apotonick/roar world. Here we go!
h3. Roar Speaks JSON-HAL Now!
In the last “Ruby on REST post”:http://nicksda.apotomo.de/2012/03/ruby-on-rest-5-learn-hypermedia-by-making-fruit-salad/ I explained the basics of the hypermedia type “HAL”:http://stateless.co/hal_specification.html which *gives your document nesting and linking* semantics. This promising format is now supported by Roar.
By including the @Representer::JSON::HAL@ module in your representer it will render and parse documents according to the HAL standard.
module OrderRepresenter include Roar::Representer::JSON::HAL property :id collection :items, :class => Item, :extend => ItemRepresenter, :embedded => true link :self do "http://orders/#{id}" end end
Links will now be pushed into the @_links@ property. Also note the new @:embedded@ option which will key the nested property under @_embedded@. And all that just by including a module – that’s the power of representers.
h3. Better Links in JSON
“Some users asked”:https://github.com/apotonick/roar/issues/15 for another link format when _not_ using the HAL representer. Usually links are handled like this.
"links":[ { "rel": "self", "href": "http://orders/42"}, { "rel": "items", "href": "http://orders/42/items"} ]}
This is just a format I came up with when writing Roar. Now, if you include @Representer::JSON::HAL::Links@ in your representer, links will be rendered and parsed a bit different.
"links": { "self": {"href": "http://orders/42"}, "items": {"href": "http://orders/42/items"} }
Here, @links@ is a hash keyed by @rel@ – very handy on the client side.
Also, you can now include more attributes in links.
link :rel => :self, :title => "That's me!"
Just provide a hash listing the attributes.
h3. Representable Got Conditional Properties!
Last but not least, “representable”:https://github.com/apotonick/representable, the underlying mapping gem of Roar got a new option in the 1.1.6 release. @:if@ allows you to define blocks that are evaled when parsing or rendering (“representing”, yo!) the object. If the proc returns false, the property is ignored.
property :state, :if => lambda { authorized? }
The block is executed in instance context allowing you to access instance methods and whatever else you need. Go for it!
}}}