Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Writing a custom YAML emitter

Reply
Thread Tools

Writing a custom YAML emitter

 
 
Ben Giddings
Guest
Posts: n/a
 
      07-28-2004
Hi all,

I'm trying to understand how to write a custom YAML emitter. As fun
and interesting as Why's documentation is, I don't quite get how to do
what I need.

What I'm looking for is a way of avoiding emitting certain instance
variables. Say I have a class like this:

class Book
attr_accessor :title, :chapter_titles
attr_accessor :current_page
end

When I save it, I want to save the permanent features like "title" and
"chapter_titles", but I want to forget the current page.

I think the YAML representation would be something like:

--- !ruby/object:Bar
title: Fun with YAML
chapter_titles:
- your first yaml document
- daily yaml chores
- advanced yaml fun

From looking at "rubytypes.rb", it looks like if I redefine
"to_yaml_properties" to return ["@title", "@chapter_titles"] it should
work. But is this the right way? Is there another preferred way?

Ben



 
Reply With Quote
 
 
 
 
why the lucky stiff
Guest
Posts: n/a
 
      07-28-2004
Ben Giddings wrote:
>
> From looking at "rubytypes.rb", it looks like if I redefine
> "to_yaml_properties" to return ["@title", "@chapter_titles"] it should
> work. But is this the right way? Is there another preferred way?
>


That's it. The to_yaml_properties is generally what I use 90% of the time.

If you decide you want to write a full-blown to_yaml method, I'd look at
the to_yaml methods in rubytypes.rb to get an idea of how it's done, as
the deep-down-to-the-black-black-sea-floor API isn't documentated at all.

_why



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] PyYAML-3.04: YAML parser and emitter for Python Kirill Simonov Python 0 08-20-2006 06:40 PM
[ANN] PyYAML-3.03: YAML parser and emitter for Python Kirill Simonov Python 0 06-19-2006 06:17 PM
[ANN] PyYAML-3.02: YAML parser and emitter for Python Kirill Simonov Python 1 05-15-2006 04:09 PM
[ANN] PyYAML-3.01: YAML parser and emitter for Python Kirill Simonov Python 0 05-08-2006 11:25 AM



Advertisments