
"built in" to_yaml method gives anonymous class error
Reported by Robert Sköld | August 23rd, 2009 @ 01:53 PM
Running this simple model in TextMate and cmd-r:
require "rubygems"
require "dm-core"
class Test
include DataMapper::Resource
property :id, Serial
property :title, String, :length => 1..255
property :url, String, :length => 1..255
end
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/test.db")
DataMapper.auto_migrate!
Test.create( :title => "hello there" , :url => "http stuff" ).to_yaml
Throws this error:
TypeError: can't dump anonymous class Class
Ideally it should return a serialized yaml, something like this:
- !ruby/object: Test
title: hello there
url: http stuff
Comments and changes to this ticket
-
Martin Gamsjaeger (snusnu) August 23rd, 2009 @ 02:55 PM
- State changed from new to confirmed
- Milestone set to 0.10.0
-
Martin Gamsjaeger (snusnu) August 23rd, 2009 @ 02:56 PM
If you add require "dm-serializer" it works as expected. Not sure if that should be required in this case though.
-
Martin Gamsjaeger (snusnu) August 23rd, 2009 @ 02:59 PM
Robert,
I deleted the other duplicates. I'm not sure, I think you need privileges to be able to delete a ticket.
-
Robert Sköld August 23rd, 2009 @ 03:29 PM
Thanks Martin,
I think what's expected would be the same as running to_yaml on a regular class?
class TestClass attr_accessor :id, :title, :url def initialize( args ) args.each { |key,val| instance_variable_set( "@#{key}" , val ) } end end p TestClass.new( :id => 1 , :title => "hello there", :url => "http stuff" ).to_yaml => "--- !ruby/object:TestClass \nid: 1\ntitle: hello there\nurl: http stuff\n"
-
JHStatewide September 30th, 2009 @ 06:05 PM
Here's a temporary workaround, if you're really stuck (as I was). By no means is this a final solution but hopefully it will suffice until a real fix is posted.
class Class
def to_yaml( opts = {} ) YAML.quick_emit(self.object_id, opts) do |out| out.map("!clazz") do |map| end end end
end
-
Dan Kubb (dkubb) October 4th, 2009 @ 09:33 PM
- Milestone changed from 0.10.0 to 0.10.2
[project:id#20609 not-tagged:"0.10.0" milestone:id#51895 bulk edit command]
-
Dan Kubb (dkubb) October 6th, 2009 @ 01:19 PM
- Milestone cleared.
- State changed from confirmed to not-applicable
A DataMapper resource is not the same as a regular instance internally. We try hard to make it so the external representation of a Resource looks like a normal instance, but handling to_yaml without dm-serializer would require us to restructure it internally.
YAML makes alot of assumptions about the internal structure of the objects that isn't always true, which is why we made the dm-serializer plugin that adds a proper to_yaml method to all resources.
If you want to submit a patch that shows how YAML can be supported with minimal restructuring of Resource, I'll review and accept it if it's simple, otherwise my recommendation for this case is to use dm-serializer to get a proper Resource#to_yaml.
Please Sign in or create a free account to add a new ticket.
With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »