Skip to content
Nick Sutterer edited this page Jul 4, 2013 · 1 revision

Object Property Without Extending

Sometimes you might have a property that is already set up for representable (responding to to_/from_hash). However, without providing either :extend or :class representable doesn't know (yet!) that it has to call the appropriate method.

property :song, instance: lambda { |*| song }

This will exactly do what you want:

  • When rendering, it treats the song as a real object and calls song.to_hash.
  • When parsing, it does not create a new instance but uses the existing song as the lambda is executed in represented context. Representable then calls song.from_hash(..) and thus "updates" the existing song instance.
Clone this wiki locally