Just a quick round-up for new things in the Roar world. Here we go!
Roar Speaks JSON-HAL Now!
In the last Ruby on REST post I explained the basics of the hypermedia type HAL 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.
Better Links in JSON
Some users asked 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.
Representable Got Conditional Properties!
Last but not least, 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!
