Serializing an instance with arbitrary methods (in rails)

Discussion in 'Ruby' started by Gavin Kistner, Nov 17, 2005.

  1. (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. :)

    --
    (-, /\ \/ / /\/
     
    Gavin Kistner, Nov 17, 2005
    #1
    1. Advertising

  2. 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
     
    James Edward Gray II, Nov 17, 2005
    #2
    1. Advertising

  3. Gavin Kistner

    Phrogz Guest

    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.)
     
    Phrogz, Nov 17, 2005
    #3
  4. Gavin Kistner

    Phrogz Guest

    >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.
     
    Phrogz, Nov 17, 2005
    #4
  5. Gavin Kistner

    Kirk Haines Guest

    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
     
    Kirk Haines, Nov 17, 2005
    #5
  6. Gavin Kistner

    Kevin Brown Guest

    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.
     
    Kevin Brown, Nov 17, 2005
    #6
  7. 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
     
    Ezra Zygmuntowicz, Nov 17, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honestmath
    Replies:
    5
    Views:
    577
    Honestmath
    Dec 13, 2004
  2. Alfonso Morra
    Replies:
    5
    Views:
    406
    Michael Wojcik
    Oct 4, 2005
  3. Alfonso Morra
    Replies:
    0
    Views:
    398
    Alfonso Morra
    Oct 3, 2005
  4. Leon Bogaert
    Replies:
    19
    Views:
    346
    Robert Klemme
    Mar 23, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    353
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page