Here is the update API
include Persistence
persistent 'people', 'employees', 'children'
person = Person.new('Alan')
employee = Employee.new('Bob', 12345)
txn
people << person
employees << employee
person.children = Array.new
employee.children = Array.new
person.children << Person.new('Jesse')
person.children << Person.new('Blake')
employee.children << Person.new('Ben')
children << person.children.concat(employee.children)
commit
{code}
READING PERSISTENT OBJECTS
include Persistence
persistent :people, :employees, :children
person = people[0]
employee = employees[0]
puts 'Person name = ' + person.name
person.children.each {|child| puts 'child = ' << child.name}
puts
puts 'Employee name = ' + employee.name
puts 'Employee ssn = ' + employee.ssn.to_s
employee.children.each {|child| puts "child = #{child.name}" }
allChildren = children[0]
allChildren.each {|child| puts "child = #{child.name}"}
OUTPUT (FROM ABOVE EXECUTION)
Person name = Alan
child = Jesse
child = Blake
child = Ben
Employee name = Bob
Employee ssn = 12345
child = Ben
child = Jesse
child = Blake
child = Ben
REGARDING THE ATTR_ACCESSOR ... I did it this way before I got your
email (isn't Ruby wonderful?)
def persistent(rootName, *others)
rootObject = nil
allNames = []
allRoots = []
allNames << rootName
allNames.concat(others)
@@session.begin
allNames.each do |name|
begin
rootObject = @@session.getInitialContext.lookup(name.to_s)
rescue
rootObject = PersistentRoot.new
@@session.getInitialContext.bind(name.to_s, rootObject)
end
eval("def [EMAIL PROTECTED]")
instance_variable_set("@#{name.to_s}", rootObject)
end
@@session.commit
end
I will look at it a bit more to see if I can clean it up. I would
like to have only a getter, so I will try using attr_reader.
(Pardon my lack of experience doing this stuff. If you see a way to
improve what I am suggesting, please let me know).
I will be working on extending the API to incorporate features like
indexing, dynamic finders, validations, sessions, etc. I don't think
it will take a lot of doing because the support for it is already in
our product. All I have to do is design a simple Ruby API for it.
Thanks
Alan
On Jul 16, 2007, at 1:42 PM, Charles Oliver Nutter wrote:
Alan McKean wrote:
I like your suggestions to make it more Rubyish. I will fix it
up.I only did it (created an instance variable @people) because I
knew how to do it. Is people (in your example) a local variable
and is there an API for creating locals?
people gets created as an attribute:
class Foo
attr_accessor :people
end
Foo.new.people = blah # calls Foo#people=(arg) to assign @people
puts Foo.new.people # calls Foo#people to retrieve @people
Your persistent method would do something similar...
def persistent(symbol)
self.class.class_eval {
attr_accessor symbol
}
name = symbol.to_s
...
And then in the actual code:
persistent :people
people.store(person)
Note also that attributes need to be prefixed with "self." or they
will be treated as local variables...but it doesn't look like your
code would be setting people often:
self.people = something # ok, calls attr setter
people = something # not ok, people is a local var
>> people << person
>
> This, too.
You may or may not want this. It works for me, but a lot of people
will want the more explicit "store".
> Although the serialization is meant for any vm, the persistence only
> works in ours. It's a certified JVM that we licensed from Sun and
has
> low-level tweaks for transparent persistence. It is based on a
shared
> page cache. Objects that are put into the page cache are faulted
in and
> out on demand.
>
> We intend to make a vm available along the same lines as we are
doing
> with the Seaside product: free for one server and up to 4 gig of
data.
> After that, its a few thousand (price to be determined). For many
> applications, the free version will be perfect.
>
> We are currently in early alpha stage with our Java 1.6 port of the
> product, so it is not generally available yet. When it reaches beta
> (about a month), we will be happy to distribute it to the dev group.
Very interesting! I look forward to seeing it soon :)
- Charlie
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email