Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Created August 6, 2013 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsutphin/6165202 to your computer and use it in GitHub Desktop.
Save rsutphin/6165202 to your computer and use it in GitHub Desktop.
Attempting to use an inline representer with Representable 1.6.0
require 'pp'
require 'representable/json'
class Album < Struct.new(:name, :songs); end
class Song < Struct.new(:title, :track); end
module AlbumPresenter
include Representable::JSON
property :name
collection :songs, class: Song do
property :title
property :track
end
end
album = Album.new('Lincoln', [
Song.new('Ana Ng', 1),
Song.new('Cowtown', 2),
Song.new('Lie Still, Little Bottle', 3)
])
pp album.extend(AlbumPresenter).to_json
$ ruby inline_representer.rb
/Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:139:in `serialize': undefined method `to_hash' for #<struct Song title="Ana Ng", track=1> (NoMethodError)
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/bindings/hash_bindings.rb:54:in `block in serialize_for'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/bindings/hash_bindings.rb:54:in `collect'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/bindings/hash_bindings.rb:54:in `serialize_for'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/bindings/hash_bindings.rb:38:in `write'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:57:in `write_fragment_for'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:52:in `write_fragment'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:36:in `block in compile_fragment'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:93:in `represented_exec_for'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/binding.rb:35:in `compile_fragment'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/mapper.rb:67:in `compile_fragment'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/mapper.rb:35:in `serialize_property'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/feature/readable_writeable.rb:11:in `serialize_property'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/mapper.rb:25:in `block in serialize'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/mapper.rb:24:in `each'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/mapper.rb:24:in `serialize'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable.rb:28:in `create_representation_with'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/hash.rb:38:in `to_hash'
from /Users/rsutphin/.rvm/gems/ruby-1.9.3-p448/gems/representable-1.6.0/lib/representable/json.rb:40:in `to_json'
from inline_representer.rb:24:in `<main>'
@rsutphin
Copy link
Author

rsutphin commented Aug 6, 2013

See apotonick/representable#54 for the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment