Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Serializing an instance with arbitrary methods (in rails)

Reply
Thread Tools

Serializing an instance with arbitrary methods (in rails)

 
 
Gavin Kistner
Guest
Posts: n/a
 
      11-17-2005
(I know there's a rails mailing list. For some reason this feels more
appropriate to ruby-talk. I'll go to the other ML if this one fails

Summary:
I'm writing a rails app where I want to save information about a
user's session, so that they may return at a later date and pick up
where they left off. The information being saved is an object with
arbitrary instance variables. What's the best way (or any good way)
to save this? So far, the only thing I can think of is Marshal dump/
load to a db row, but this seems off for some reason. Is this the
best way?

Details:
I'm writing an app that's an odd mixture of interactive fiction,
wiki, and MUD. (Well, single-user for now.) Think of a web version of
a "Choose Your Own Adventure" book, where anyone can write the pages/
choices, and every page and choice can have arbitrary ruby code
associated with it. Each page and choice is an ERB template (in the
future, with wiki markup), allowing the contents of the story and
text of the choices to vary.

Each page can run Ruby code against a 'ReaderSession' before the page
is viewed, setting the consequences of getting to the page. Each
choice can run Ruby code to decide if it may be shown (e.g. has the
user already made this choice? does the user have enough gold to buy
the rabbit? does the user have the sword needed to slay the dragon
here?). When the user makes a choice, code specific to that choice
can be run before the destination page is shown (e.g. subtract the
right amount of gold and add the rabbit to the inventory).

When the user starts a story, a blank ReaderSession object is
created. The code for each page/choice is run within the scope of
this instance, so that any instance variables may be set without
harming others (a sort of namespace) and I can easily start the story
over by clearing out the instance variables.

Certain details haven't been ironed out yet: should each choice be
hardcoded to a destination page, or can the 'result code' for a
choice change which page you go to? How will I use AJAX to make this
a multi-user/reader game, so that when someone else arrives at the
same page, you know they're there? Can pages have time limits that
force the user to make a choice before a default action is taken
automatically?

Thanks for any advice/brainstorming. As with all my personal
projects, this will be an (advertisement-free), free service for
the .net to enjoy. (In other words, helping me isn't helping with
some commercial project you'll never see.

--
(-, /\ \/ / /\/


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      11-17-2005
On Nov 17, 2005, at 10:21 AM, Gavin Kistner wrote:

> Summary:
> I'm writing a rails app where I want to save information about a
> user's session, so that they may return at a later date and pick up
> where they left off. The information being saved is an object with
> arbitrary instance variables. What's the best way (or any good way)
> to save this? So far, the only thing I can think of is Marshal dump/
> load to a db row, but this seems off for some reason. Is this the
> best way?


What's wrong with just sticking the object in the session?

James Edward Gray II


 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      11-17-2005
James Edward Gray II wrote:
> What's wrong with just sticking the object in the session?


Erps, did I forget to include that? I'd like a user to be able to
return to the story after quitting/restarting/exploding the computer,
and resume with the ReaderSession where s/he left off. Unless I'm
missing an option in Rails, sessions go away when the session ends.
(The cookie identifier is set to expire when the browser closes.)

 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      11-17-2005
>Unless I'm missing an option in Rails,
> sessions go away when the session ends.
> (The cookie identifier is set to expire when the browser closes.)


Yeah, apparently I was missing something. Or at least, further
searching is showing me words that appear to be related to what I want
to do.

Sorry for the noise. RTFM and all that.

 
Reply With Quote
 
Kirk Haines
Guest
Posts: n/a
 
      11-17-2005
On Thursday 17 November 2005 9:21 am, Gavin Kistner wrote:
> (I know there's a rails mailing list. For some reason this feels more
> appropriate to ruby-talk. I'll go to the other ML if this one fails
>
> Summary:
> I'm writing a rails app where I want to save information about a
> user's session, so that they may return at a later date and pick up
> where they left off. The information being saved is an object with
> arbitrary instance variables. What's the best way (or any good way)
> to save this? So far, the only thing I can think of is Marshal dump/
> load to a db row, but this seems off for some reason. Is this the
> best way?


Use Marshal for this. YAML offers similar capabilities, but it is MUCH
slower, doesn't handle circular references and such as well, and historically
has had bizarre corner cases pop up that broke it. It may be that those
corner cases are all resolved, but unless you need a human readable
serialization format, use Marshal.


Kirk Haines


 
Reply With Quote
 
Kevin Brown
Guest
Posts: n/a
 
      11-17-2005
On Thursday 17 November 2005 11:22, Kirk Haines wrote:
> Use Marshal for this. YAML offers similar capabilities, but it is MUCH
> slower, doesn't handle circular references and such as well, and
> historically has had bizarre corner cases pop up that broke it. It may be
> that those corner cases are all resolved, but unless you need a human
> readable serialization format, use Marshal.


Or unless you're also using DRb on the same objects and need to have them
DRbUndumped.


 
Reply With Quote
 
Ezra Zygmuntowicz
Guest
Posts: n/a
 
      11-17-2005

On Nov 17, 2005, at 8:21 AM, Gavin Kistner wrote:

> (I know there's a rails mailing list. For some reason this feels
> more appropriate to ruby-talk. I'll go to the other ML if this one
> fails
>
> Summary:
> I'm writing a rails app where I want to save information about a
> user's session, so that they may return at a later date and pick up
> where they left off. The information being saved is an object with
> arbitrary instance variables. What's the best way (or any good way)
> to save this? So far, the only thing I can think of is Marshal dump/
> load to a db row, but this seems off for some reason. Is this the
> best way?
>
> <snip>
> (-, /\ \/ / /\/
>
>



Gavin-

There is a macro in rails for serializing an object into one column
in your db table. It uses yaml and once you set it it will
automatically serialize and unserialize when you save/retrieve it
form the db. I think this is what you want. It works like so:

In your model you define which column to serialize and what type of
object to re-hydrate after retrieving it from the db:

class MyModel < ActiveRecord::Base
serialize :notes, :class_name => "Hash"
end


And thats all there is to it.

HTH-

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
http://www.velocityreviews.com/forums/(E-Mail Removed)





 
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
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
including instance methods and setting an instance variable Leon Bogaert Ruby 19 03-23-2008 09:29 PM
serializing an arbitrary data structure into a flat buffer (raw contiguousmemory block) Alfonso Morra C Programming 5 10-04-2005 01:55 PM
serializing an arbitrary data structure into a flat buffer (raw contiguousmemory block) Alfonso Morra C++ 0 10-03-2005 07:13 AM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM



Advertisments