forked from Empact/roxml
-
Notifications
You must be signed in to change notification settings - Fork 108
Tips Tricks
Nick Sutterer edited this page Jul 4, 2013
·
1 revision
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
songas a real object and callssong.to_hash. - When parsing, it does not create a new instance but uses the existing
songas the lambda is executed in represented context. Representable then callssong.from_hash(..)and thus "updates" the existingsonginstance.