README.md in representable-1.3.5 vs README.md in representable-1.4.0
- old
+ new
@@ -53,10 +53,16 @@
It also adds support for parsing.
song = Song.new.extend(SongRepresenter).from_json(%{ {"title":"Roxanne"} })
#=> #<Song title="Roxanne", track=nil>
+
+## Extend vs. Decorator
+
+If you don't want representer modules to be mixed into your objects (using `#extend`) you can use the `Decorator` strategy [described below](https://github.com/apotonick/representable#decorator-vs-extend). Decorating instead of extending was introduced in 1.4.
+
+
## Aliasing
If your property name doesn't match the name in the document, use the `:as` option.
module SongRepresenter
@@ -144,9 +150,33 @@
Album.new.extend(AlbumRepresenter).
from_json(%{{"name":"Offspring","songs":[{"title":"Genocide"},{"title":"Nitro","composers":["Offspring"]}]}})
#=> #<Album name="Offspring", songs=[#<Song title="Genocide">, #<Song title="Nitro", composers=["Offspring"]>]>
+## Decorator vs. Extend
+
+People who dislike `:extend` go use the `Decorator` strategy!
+
+ class SongRepresentation < Representable::Decorator
+ include Representable::JSON
+
+ property :title
+ property :track
+ end
+
+The `Decorator` constructor requires the represented object.
+
+ SongRepresentation.new(song).to_json
+
+This will leave the `song` instance untouched as the decorator just uses public accessors to represent the hit.
+
+In compositions you need to specify the decorators for the nested items using the `:decorator` option where you'd normally use `:extend`.
+
+ class AlbumRepresentation < Representable::Decorator
+ include Representable::JSON
+
+ collection :songs, :class => Song, :decorator => SongRepresentation
+ end
## XML Support
While representable does a great job with JSON, it also features support for XML, YAML and pure ruby hashes.