README.md in representable-1.3.2 vs README.md in representable-1.3.3

- old
+ new

@@ -170,11 +170,39 @@ <title>Fallout</title> <composers>Steward Copeland</composers> <composers>Sting</composers> </song> +## Passing Options +You're free to pass an options hash into the rendering or parsing. + + song.to_json(:append => "SOLD OUT!") + +If you want to append the "SOLD OUT!" to the song's `title` when rendering, use the `:getter` option. + + SongRepresenter + include Representable::JSON + + property :title, :getter => lambda { |args| title + args[:append] } + end + +Note that the block is executed in the represented model context which allows using accessors and instance variables. + + +The same works for parsing using the `:setter` method. + + property :title, :setter => lambda { |val, args| self.title= val + args[:append] } + +Here, the block retrieves two arguments: the parsed value and your user options. + +You can also use the `:getter` option instead of writing a reader method. Even when you're not interested in user options you can still use this technique. + + property :title, :getter => lambda { |*| @name } + +This hash will also be available in the `:if` block, documented [here](https://github.com/apotonick/representable/#conditions) and will be passed to nested objects. + ## Using Helpers Sometimes it's useful to override accessors to customize output or parsing. module AlbumRepresenter @@ -415,11 +443,10 @@ ## More Options Here's a quick overview about other available options for `#property` and its bro `#collection`. - ### Read/Write Restrictions Using the `:readable` and `:writeable` options access to properties can be restricted. property :title, :readable => false @@ -446,11 +473,19 @@ property :track, if: lambda { track > 0 } end When rendering or parsing, the `track` property is considered only if track is valid. Note that the block is executed in instance context, giving you access to instance methods. +As always, the block retrieves your options. Given this render call + song.to_json(minimum_track: 2) + +your `:if` may process the options. + + property :track, if: lambda { |opts| track > opts[:minimum_track] } + + ### False and Nil Values Since representable-1.2 `false` values _are_ considered when parsing and rendering. That particularly means properties that used to be unset (i.e. `nil`) after parsing might be `false` now. Vice versa, `false` properties that weren't included in the rendered document will be visible now. If you want `nil` values to be included when rendering, use the `:render_nil` option. @@ -472,9 +507,18 @@ include Representable::Coercion property :title property :recorded_at, :type => DateTime, :default => "May 12th, 2012" end + + +## Undocumented Features + +(Please don't read this section!) + +If you need a special binding for a property you're free to create it using the `:binding` option. + + property :title, :binding => lambda { |*args| JSON::TitleBinding.new(*args) } ## Copyright Representable started as a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his inspiring work.