README.md in representable-1.8.5 vs README.md in representable-2.0.0.rc1
- old
+ new
@@ -2,11 +2,11 @@
Representable maps Ruby objects to documents and back.
In other words: Take an object and decorate it with a representer module. This will allow you to render a JSON, XML or YAML document from that object. But that's only half of it! You can also use representers to parse a document and create or populate an object.
-Representable is helpful for all kind of rendering and parsing workflows. However, it is mostly useful in API code. Are you planning to write a real REST API with representable? Then check out the [Roar](http://github.com/apotonick/roar) gem first, save work and time and make the world a better place instead.
+Representable is helpful for all kind of mappings, rendering and parsing workflows. However, it is mostly useful in API code. Are you planning to write a real REST API with representable? Then check out the [Roar](http://github.com/apotonick/roar) gem first, save work and time and make the world a better place instead.
## Installation
The representable gem runs with all Ruby versions >= 1.8.7.
@@ -265,11 +265,11 @@
end
```
This works both for representer modules and decorators.
-An inline representer is just a Ruby module. You can include other representer modules. This is handy when having a base representer that needs to be extended in the inline block.
+An inline representer is just a Ruby module (or a `Decorator` class). You can include other representer modules. This is handy when having a base representer that needs to be extended in the inline block.
```ruby
module AlbumRepresenter
include Representable::JSON
@@ -277,9 +277,69 @@
include SongRepresenter
property :numbers_sold
end
```
+
+If you need to include modules in all inline representers automatically, register it as a feature.
+
+```ruby
+module AlbumRepresenter
+ include Representable::JSON
+ feature Link # imports ::link
+
+ link "/album/1"
+
+ property :hit do
+ link "/hit/1" # link method imported automatically.
+ end
+```
+
+
+## Representing Singular Models And Collections
+
+You can explicitly define representers for collections of models using a ["Lonely Collection"](#lonely-collections). Or you can let representable do that for you.
+
+Rendering a collection of objects comes for free, using `::for_collection`.
+
+```ruby
+ Song.all.extend(SongRepresenter.for_collection).to_hash
+ #=> [{title: "Sevens"}, {title: "Eric"}]
+```
+
+For parsing, you need to provide the class constant to which the items should be deserialized to.
+
+```ruby
+module SongRepresenter
+ include Representable::Hash
+ property :title
+
+ collection_representer class: Song
+end
+```
+
+You can now parse collections to `Song` instances.
+
+```ruby
+[].extend(SongRepresenter.for_collection).from_hash([{title: "Sevens"}, {title: "Eric"}])
+```
+
+As always, this works for decorators _and_ modules.
+
+In case you don't want to know whether or not you're working with a collection or singular model, use `::represent`.
+
+```ruby
+# singular
+SongRepresenter.represent(Song.find(1)).to_hash #=> {title: "Sevens"}
+
+# collection
+SongRepresenter.represent(Song.all).to_hash #=> [{title: "Sevens"}, {title: "Eric"}]
+```
+
+As you can see, `::represent` figures out the correct representer for you (works also for parsing!).
+
+Note: the implicit collection representer internally is implemented using a lonely collection. Everything you pass to `::collection_representer` is simply provided to the `::items` call in the lonely collection. That allows you to use `:parse_strategy` and all the other goodies, too.
+
## Document Nesting
Not always does the structure of the desired document map to your objects. The `::nested` method allows you to structure properties in a separate section while still mapping the properties to the outer object.