So I come to the part of my little app where I want to load and save a 
config file. I want it to be human read/writeable too though - it's not 
just for serialization. In fact, usually a human will write it, my 
little app will read it, and may adjust it and write it out anew.

So in xml it would look like this
<channels>
  <channel>
    <name>foo</name>
    <id>3</id>
  </channel>
  <channel>
 กฤ
 </channels>

I don't care about channels that's just a random root node, and I might 
do it with attbs instead of elements, so:

<channel name="foo" id="3"/>

So I thought, lets use YAML instead - it's even easier to read and 
write. Google, google, read blogs, docs, google, google, more docs, more 
blogs, google to_yaml, fxri: hack hack, google to_yaml_type, fxri: hack, 
hack, hack, google, google, yaml_as, hack.

Argh! Seems like day later, and I'm ready to go back to xml. But I'm 
sure there's a simple way to do what I want, so I'll post here first and 
hope. See, my class is Channel, like so:

class Channel; attr_accessor :name, :id; end

and want my array of channels to look something like this

- Channel
  name: foo
  id: 3
- Channel
  name: bar
  id: 4
กฤ

But I just can't work out how to simply the yaml output to this stage. 
And I don't want to override a dozen methods and do my own hash mapping.
The easiest way - thanks to why for posting this in the forum - seems to 
be to use yaml_as:

class Channel
  yaml_as "tag:foobar,2007:Channel"
end

but then I get each entry as

 --- !foolbar,2007/Channel
   name: foo
   id: 3

This is fairly simple, but remember that I want my entries to be 
human-writeable the first time. That line of "---!foobar,2007กฤ" is just 
way too machiney for us poor humans to get right every time.

I can simplify the taguri like this
  yaml_as "tag::Channel"

to get

 --- !Channel
...

and that's about as simple as I can get it without it failing to write 
yaml. But loading it back in will fail to build the object - it will 
give ma a YAML::DynamicType instead - which is what it always gives if 
it doesn't recognize the object.
I guess the load method needs a valid taguri

(Side note - I'd never heard of a taguri before, but when I went to read 
about it at http://www.taguri.org/ I was _very_ pleasantly surprised at 
how quickly I was able to grok the whole thing by reading Sandro Hawke's 
four-paragraph tutorial about his dog Taiko. Way to go Sandro - if I'd 
been directed to the RFC first it'd be all over)

So anyway, the fact is that while taguris are great for distinguishing 
Taiko from his master's descendents' pooches, and while it's very 
simple, it's still overkill for me. I don't need a unique ID. I know 
what "--- Channel" means in my little app. I don't want to submit my 
Channel types to some great panoply of random little types each with 
their own special pet name. I just want to damn well read and write it.

I've even considered parsing the text in to_yaml and YAML.load and 
gsubbing "!foobar, 2007/" out and in again.  But there's got to be a 
better way. So far that better way is looking like xml. but there's got 
to be a better rubyful way.

What is it?

-- 
Posted via http://www.ruby-forum.com/.