apotonick/roar — Issue #9: Choose representers for nested elements on runtime
Author: chrisupb
Created: 2012-01-08 10:10:09+00:00
State: open

I wonder if it is feasible to let roar guess the representer class of a collection/property.

Imagine a generic collection class:

```ruby
class Api::Collection
include Roar::Representer
include CollectionRepresenter

def initialize(type, total=0, members=[], self_link, next_link, prev_link)
@type = type
@total = total
@members = members
@self_link = self_link
@next_link = next_link
@prev_link = prev_link
end

attr_accessor :type, :total, :members, :self_link, :next_link, :prev_link
end
```

And the corresponding representer:

```ruby
module CollectionRepresenter
include Roar::Representer::JSON
include Roar::Representer::Feature::Hypermedia

property :type
property :total

link :self do
self_link
end
link :next do
next_link
end
link :prev do
prev_link
end
collection :members #, :class => User, :extend => UserRepresenter
end
```

As _members_ could contain different objects, we cannot set :class and :extend options.
What do you think about letting roar extract the class name of a collection element and set the _sought_type_ and _representer_module_ of the corresponding ``` Definition ``` according to a configurable convention. Something like ```class.singularize+"Representer"```.