README.md in representable-1.7.0 vs README.md in representable-1.7.1
- old
+ new
@@ -172,10 +172,39 @@
from_json(%{{"name":"Offspring","songs":[{"title":"Genocide"},{"title":"Nitro","composers":["Offspring"]}]}})
#=> #<Album name="Offspring", songs=[#<Song title="Genocide">, #<Song title="Nitro", composers=["Offspring"]>]>
```
+## Syncing Objects
+
+Usually, representable creates a new nested object when parsing. If you want to update an existing object, use the `parse_strategy` option.
+
+```ruby
+module AlbumRepresenter
+ include Representable::JSON
+
+ collection :songs, extend: SongRepresenter, parse_strategy: :sync
+```
+
+When parsing an album, it will now call `from_json` on the existing songs in the collection.
+
+```ruby
+album = Album.find(1)
+album.songs.first #=> #<Song:0x999 title: "Panama">
+```
+
+Note that the album already contains a song instance.
+
+```ruby
+album.extend(AlbumRepresenter).
+ from_json('{songs: [{title: "Eruption"}]}')
+
+album.songs.first #=> #<Song:0x999 title: "Eruption">
+```
+
+Now, representable didn't create a new `Song` instance but updated the existing, resulting in renaming the song.
+
## Inline Representers
If you don't want to maintain two separate modules when nesting representations you can define the `SongRepresenter` inline.
```ruby
@@ -387,11 +416,26 @@
extend(CoverSongRepresenter).to_json
#=> {"title":"Truth Hits Everybody","copyright":"The Police"}
```
+## Overriding Properties
+You might want to override a particular property in an inheriting representer. Successively calling `property(name)` will override the former definition for `name` just as you know it from overriding methods.
+
+```ruby
+module CoverSongRepresenter
+ include Representable::JSON
+
+ include SongRepresenter # defines property :title
+ property :title, as: :known_as # overrides that definition.
+end
+```
+
+This behaviour was added in 1.7.
+
+
## Polymorphic Extend
Sometimes heterogenous collections of objects from different classes must be represented. Or you don't know which representer to use at compile-time and need to delay the computation until runtime. This is why `:extend` accepts a lambda, too.
Given we not only have songs, but also cover songs.
@@ -590,9 +634,20 @@
<song>Laundry Basket</song>
<song>Two Kevins</song>
<song>Wright and Rong</song>
</songs>
</album>
+```
+
+### Namespaces
+
+Support for namespaces are not yet implemented. However, if an incoming parsed document contains namespaces, you can automatically remove them.
+
+```ruby
+module AlbumRepresenter
+ include Representable::XML
+
+ remove_namespaces!
```
## Avoiding Modules
There's been a rough discussion whether or not to use `extend` in Ruby. If you want to save that particular step when representing objects, define the representers right in your classes.