Representable also comes with a YAML representer. Like XML, the declarative API is almost identical.
Flow Style Lists
A nice feature is that
#collection also accepts a
:style option which helps having nicely formatted inline (or “flow”) arrays in your YAML - if you want that!
require 'representable/yaml' class SongRepresenter < Representable::Decorator include Representable::YAML property :title property :id collection :composers, style: :flow end
To render and parse, you invoke
Song = Struct.new(:title, :id, :composers) song = Song.new("Fallout", 1, ["Stewart Copeland", "Sting"]) SongRepresenter.new(song).to_yaml
--- title: Fallout id: 1 composers: [Stewart Copeland, Sting]