On Sat, 26 Apr 2003, Matt Lawrence wrote:

> On Fri, 25 Apr 2003, Ryan Pavlik wrote:
>
> > If you can hang on for a day or so, I'll release Mephle on Monday.
> > I just added the ability to write non-networked apps a few nights ago,
> > along with a SQLite storage driver.
>
> Well, it sounds a bit complex, but I can certainly wait.  The big thing I
> want is to be able to say something like 'arr.push "foo"' and it will be
> persistant.

none too fast - but this will do it:

~/eg/ruby > cat pobject.rb
require 'pstore'

class PObject
  DEFAULT_STORE = File.basename($0) << '.pstore'

  attr :object

  def initialize key, pstore = PStore.new (DEFAULT_STORE)
    @key = key
    @pstore = pstore
    @object = nil

    @pstore.transaction do
      @object = @pstore[@key] rescue nil
    end

    @object ||= yield
  end

  def method_missing (method, *args, &block)
    @object.send (method, *args, &block)
    persist
  end

  def persist
    @pstore.transaction do
      @pstore[@key] = @object
    end
  end
end


~/eg/ruby > cat pobjectdemo.rb
#!/usr/bin/env ruby

require 'pobject'

a = PObject.new(:a) { Array.new  }
a << Time.now

a.each do |time|
  puts time
end


~/eg/ruby > pobjectdemo.rb
Fri Apr 25 22:48:37 GMT 2003

~/eg/ruby > pobjectdemo.rb
Fri Apr 25 22:48:37 GMT 2003
Fri Apr 25 22:48:39 GMT 2003

~/eg/ruby > pobjectdemo.rb
Fri Apr 25 22:48:37 GMT 2003
Fri Apr 25 22:48:39 GMT 2003
Fri Apr 25 22:48:40 GMT 2003


-a

--
  ====================================
  | Ara Howard
  | NOAA Forecast Systems Laboratory
  | Information and Technology Services
  | Data Systems Group
  | R/FST 325 Broadway
  | Boulder, CO 80305-3328
  | Email: ara.t.howard / fsl.noaa.gov
  | Phone:  303-497-7238
  | Fax:    303-497-7259
  ====================================