From: why the lucky stiff Date: 2004-11-27T01:36:46+09:00 Subject: Re: YAML question (hi _why!) Hal Fulton wrote: > This property_map isn't clear to me yet. Why not class-level data instead > of instance? You could certainly use a class method. In Hobix, I like to encourage the user to override my classes and to create singletons to exhibit custom behavior. Instance methods are simpler in this regard. > I hadn't thought about the issue of required vs. optional. I would have > just said: If it doesn't have a default, and it's not there, default it > to nil. Having required fields is nice when I am supplying the user with a blank object to fill in. >> As you can see, the mixin also defines 'initialize', since you'll >> probably want to apply the defaults even if the object is created >> programmatically. > > This confuses me greatly. What if a class already has an #initialize (as > most do?). I'm providing this default #initialize as an example of using #apply_defaults in the constructor. It's okay if it's not used. >> You'll also need to hook yourself a YAML type for each class. In the >> case of the Hobix::Entry class, I'm hooking !hobix.com,2004/entry. > > Argh. If I had to do this stuff every time, I'd almost rather keep using > the hack I'm using. If it's any reassurance, this will be simpler in Syck 0.50. You'll still have to set up the property_map and use the mixin to get this auto-defaults behavior. But assigning types will be as simple as: class Hobix::Entry tag_as "tag:hobix.com,2004:entry" end For now, though, you'll need to do this for each class with a type: class Hobix::Entry def to_yaml_type "!hobix.com,2004/entry" end end YAML::add_domain_type( 'hobix.com,2004', 'entry' ) do |type, val| YAML::object_maker( Hobix::Entry, val ).apply_defaults end _why