1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
require 'bundler'
Bundler.setup
require 'representable/yaml'
require 'ostruct'
def reset_representer(*module_name)
module_name.each do |mod|
mod.module_eval do
@representable_attrs = nil
end
end
end
class Song < OpenStruct
end
song = Song.new(:title => "Fallout", :track => 1)
module SongRepresenter
include Representable::JSON
property :title
property :track
end
puts song.extend(SongRepresenter).to_json
rox = Song.new.extend(SongRepresenter).from_json(%{ {"title":"Roxanne"} })
puts rox.inspect
module SongRepresenter
include Representable::JSON
self.representation_wrap= :hit
property :title
property :track
end
puts song.extend(SongRepresenter).to_json
######### collections
reset_representer(SongRepresenter)
module SongRepresenter
include Representable::JSON
property :title
property :track
collection :composers
end
song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
puts song.extend(SongRepresenter).to_json
######### nesting types
class Album < OpenStruct
def name
puts @table.inspect
#@attributes
@table[:name]
end
end
module AlbumRepresenter
include Representable::JSON
property :name
property :song, :extend => SongRepresenter, :class => Song
end
album = Album.new(:name => "The Police", :song => song)
puts album.extend(AlbumRepresenter).to_json
reset_representer(AlbumRepresenter)
module AlbumRepresenter
include Representable::JSON
property :name
collection :songs, :extend => SongRepresenter, :class => Song
end
album = Album.new(:name => "The Police", :songs => [song, Song.new(:title => "Synchronicity")])
puts album.extend(AlbumRepresenter).to_json
album = Album.new.extend(AlbumRepresenter).from_json(%{{"name":"Offspring","songs":[{"title":"Genocide"},{"title":"Nitro","composers":["Offspring"]}]}
})
puts album.inspect
reset_representer(SongRepresenter)
######### using helpers (customizing the rendering/parsing)
module AlbumRepresenter
include Representable::JSON
def name
super.upcase
end
end
puts Album.new(:name => "The Police").
extend(AlbumRepresenter).to_json
reset_representer(SongRepresenter)
######### inheritance
module SongRepresenter
include Representable::JSON
property :title
property :track
end
module CoverSongRepresenter
include Representable::JSON
include SongRepresenter
property :covered_by
end
song = Song.new(:title => "Truth Hits Everybody", :covered_by => "No Use For A Name")
puts song.extend(CoverSongRepresenter).to_json
### XML
require 'representable/xml'
module SongRepresenter
include Representable::XML
property :title
property :track
collection :composers
end
song = Song.new(:title => "Fallout", :composers => ["Stewart Copeland", "Sting"])
puts song.extend(SongRepresenter).to_xml
reset_representer(SongRepresenter)
### YAML
require 'representable/yaml'
module SongRepresenter
include Representable::YAML
property :title
property :track
collection :composers
end
puts song.extend(SongRepresenter).to_yaml
SongRepresenter.module_eval do
@representable_attrs = nil
end
### YAML
module SongRepresenter
include Representable::YAML
property :title
property :track
collection :composers, :style => :flow
end
puts song.extend(SongRepresenter).to_yaml
SongRepresenter.module_eval do
@representable_attrs = nil
end
# R/W support
song = Song.new(:title => "You're Wrong", :track => 4)
module SongRepresenter
include Representable::Hash
property :title, :readable => false
property :track
end
puts song.extend(SongRepresenter).to_hash
SongRepresenter.module_eval do
@representable_attrs = nil
end
module SongRepresenter
include Representable::Hash
property :title, :writeable => false
property :track
end
song = Song.new.extend(SongRepresenter)
song.from_hash({:title => "Fallout", :track => 1})
puts song
######### custom methods in representer (using helpers)
######### conditions
######### :as
######### XML::Collection
######### polymorphic :extend and :class, instance context!, :instance
class CoverSong < Song
end
songs = [Song.new(title: "Weirdo", track: 5), CoverSong.new(title: "Truth Hits Everybody", track: 6, copyright: "The Police")]
album = Album.new(name: "Incognito", songs: songs)
reset_representer(SongRepresenter, AlbumRepresenter)
module SongRepresenter
include Representable::Hash
property :title
property :track
end
module CoverSongRepresenter
include Representable::Hash
include SongRepresenter
property :copyright
end
module AlbumRepresenter
include Representable::Hash
property :name
collection :songs, :extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter }
end
puts album.extend(AlbumRepresenter).to_hash
reset_representer(AlbumRepresenter)
module AlbumRepresenter
include Representable::Hash
property :name
collection :songs,
:extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
:class => lambda { |hsh| hsh.has_key?("copyright") ? CoverSong : Song } #=> {"title"=>"Weirdo", "track"=>5}
end
album = Album.new.extend(AlbumRepresenter).from_hash({"name"=>"Incognito", "songs"=>[{"title"=>"Weirdo", "track"=>5}, {"title"=>"Truth Hits Everybody", "track"=>6, "copyright"=>"The Police"}]})
puts album.inspect
reset_representer(AlbumRepresenter)
module AlbumRepresenter
include Representable::Hash
property :name
collection :songs,
:extend => lambda { |song| song.is_a?(CoverSong) ? CoverSongRepresenter : SongRepresenter },
:instance => lambda { |hsh| hsh.has_key?("copyright") ? CoverSong.new : Song.new(original: true) }
end
album = Album.new.extend(AlbumRepresenter).from_hash({"name"=>"Incognito", "songs"=>[{"title"=>"Weirdo", "track"=>5}, {"title"=>"Truth Hits Everybody", "track"=>6, "copyright"=>"The Police"}]})
puts album.inspect
######### #hash
reset_representer(SongRepresenter)
module SongRepresenter
include Representable::JSON
property :title
hash :ratings
end
puts Song.new(title: "Bliss", ratings: {"Rolling Stone" => 4.9, "FryZine" => 4.5}).extend(SongRepresenter).to_json
######### JSON::Hash
module FavoriteSongsRepresenter
include Representable::JSON::Hash
end
puts( {"Nick" => "Hyper Music", "El" => "Blown In The Wind"}.extend(FavoriteSongsRepresenter).to_json)
require 'representable/json/hash'
module FavoriteSongsRepresenter
include Representable::JSON::Hash
values extend: SongRepresenter, class: Song
end
puts( {"Nick" => Song.new(title: "Hyper Music")}.extend(FavoriteSongsRepresenter).to_json)
require 'representable/json/collection'
module SongsRepresenter
include Representable::JSON::Collection
items extend: SongRepresenter, class: Song
end
puts [Song.new(title: "Hyper Music"), Song.new(title: "Screenager")].extend(SongsRepresenter).to_json
|