Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Concerning persistency techniques

Reply
Thread Tools

Concerning persistency techniques

 
 
Christophe Poucet
Guest
Posts: n/a
 
      09-29-2005
Hello,

I had two remarks that I thought I'd bring up. The first concerns a
discrepancy between YAML and Marshal. It has to do with the fact that
YAML will not save any classes that were extended into an object:

class Plop
attr_accessor :foo
def initialize(foo)
@foo = foo
end
end

module Plip
attr_accessor :bar
def bar
@bar || 42
end
end

a = Plop.new(42)
a.extend(Plip)
a.bar # => 42

require 'yaml'
# Looking at the string made by YAML.dump won't show any references
to Plip
b = YAML.load(YAML.dump(a))
b.bar # => NoMethodError

b = Marshal.load(Marshal.dump(a))
b.bar # => 42

The other remark I had was more of a question. I have an object
manager that tracks a whole set of objects (each with their own unique
id). This object manager is able to save and load (currently to YAML,
but that is to be changed soon). However one problem is that that all
the class-types that could be saved have to be known by this object
manager. This results into the following code:

def full_require(dirname)
Dir.foreach(dirname) {|f|
name = "#{dirname}/#{f}"
if File.directory?(name)
full_require(name) unless f =~ /^\..*/
else
require "#{name}"
end
}
end

class ObjectManager
# ...
def load(io)
full_require("lib/virtual")
# Do actual loading
end
# ...
end

Does anyone have any suggestions on how to tackle this problem
differently?

Regards,

Christophe/vincenz

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      09-29-2005
%Q{ However one problem is that that all
the class-types that could be saved have to be known by this > object
manager. This results into the following code: }

I don't see how the code follows. You have what I call "require_all"
defined, thus requiring every file in a directory. Don't see how that
relates to ObjectManager knowing the classes. Or do you just mean that
the classes must already be defined upon loading the stored objects?

T.

 
Reply With Quote
 
 
 
 
Christophe Poucet
Guest
Posts: n/a
 
      09-29-2005
> I don't see how the code follows. You have what I call "require_all"
> defined, thus requiring every file in a directory. Don't see how

that
> relates to ObjectManager knowing the classes. Or do you just mean

that
> the classes must already be defined upon loading the stored objects?



Yes, that is exactly what I mean. The classes must be defined upon
loading the stored object, which might not be the case.

Regards,

Christophe/vincenz

 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      09-29-2005
Christophe Poucet:
> The other remark I had was more of a question. I have an object
> manager that tracks a whole set of objects (each with their own unique
> id). This object manager is able to save and load (currently to YAML,
> but that is to be changed soon). However one problem is that that all
> the class-types that could be saved have to be known by this object
> manager. This results into the following code:
>
> <snip code>
>
> Does anyone have any suggestions on how to tackle this problem
> differently?


The following, taken from Ruby's docs, "implements a perverse kind of
autoload facility" which may nevertheless solve a large part of your
problem. The part that it won't solve may be solved by the addition of a
lookup table mapping class names to their respective require paths.

def Object.const_missing(name)
@looked_for ||= {}
str_name = name.to_s
raise "Class not found: #{name}" if @looked_for[str_name]
@looked_for[str_name] = 1
file = str_name.downcase
require file
klass = const_get(name)
return klass if klass
raise "Class not found: #{name}"
end

Cheers,
Dave


 
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
object persistency, store instances relationship externally King Python 2 07-25-2008 05:05 PM
Persistency problem Mauro Java 1 05-10-2007 10:00 AM
a problem concerning the computer using wireless card =?Utf-8?B?a2FyZWVt?= Wireless Networking 1 06-04-2005 02:46 PM
"Persistency Made Easy" source code and examples? Robert Buck C++ 0 02-24-2004 04:56 PM
Auto persistency Batista, Facundo Python 2 08-07-2003 10:25 AM



Advertisments