README.md in representable-1.5.1 vs README.md in representable-1.5.2

- old
+ new

@@ -216,14 +216,26 @@ def title @name end ``` -That works as the method is mixed into the represented object. Of course, this doesn't work with decorators. +That works as the method is mixed into the represented object. When adding a helper method to a decorator, representable will still invoke accessors on the represented instance - unless you tell it the scope. -Use `:getter` or `:setter` to dynamically add a method for the represented object. +```ruby +class SongRepresenter < Representable::Decorator + property :title, decorator_scope: true + def title + represented.name + end +end +``` + +This will call `title` getter and setter on the decorator instance, not on the represented object. You can still access the represented object in the decorator method using `represented`. BTW, in a module representer this option is ignored. + +Or use `:getter` or `:setter` to dynamically add a method for the represented object. + ```ruby class SongRepresenter < Representable::Decorator property :title, getter: lambda { |*| @name } ``` As always, the block is executed in the represented object's context. @@ -665,30 +677,30 @@ property :title property :recorded_at, :type => DateTime, :default => "May 12th, 2012" end ``` +When using a decorator representer, use the `Representable::Decorator::Coercion` module. + +```ruby +module SongRepresenter < Representable::Decorator + include Representable::JSON + include Representable::Decorator::Coercion + + property :recorded_at, :type => DateTime +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. <!-- here comes some code --> ```ruby property :title, :binding => lambda { |*args| JSON::TitleBinding.new(*args) } ``` - -* Lambdas are usually executed in the represented object's context. If your writing a `Decorator` representer and you need to execute lambdas in its context use the `:representer_exec` option. - -<!-- and some more in a beautiful cuddle --> -```ruby -class SongRepresenter < Representable::Decorator - property :title, :representer_exec => true, :getter => lambda {..} -end -``` - -You can still access the represented object in the lambda using `represented`. In a module representer this option is ignored. ## Copyright Representable started as a heavily simplified fork of the ROXML gem. Big thanks to Ben Woosley for his inspiring work.