class Durable

Attributes

val[RW]

Public Class Methods

commit() click to toggle source
# File lib/durable.rb, line 31
def self.commit
  @@instances.map{|x| x.commit}
end
commitAndExit() click to toggle source
# File lib/durable.rb, line 27
def self.commitAndExit
  self.commit if @@AUTOCOMMIT_MODE
end
config(config) click to toggle source
# File lib/durable.rb, line 22
def self.config(config)
  @@AUTOCOMMIT_MODE = config[:autocommit] if config[:autocommit] != nil
  @@DURABLE_HOME = config[:home] if config[:home] != nil
end
new(obj) click to toggle source
# File lib/durable.rb, line 68
def initialize(obj)
  @@instances << self
  @key = obj.keys[0]

  if(key_exists(@key) && compare_helper(load_helper(@key)[:initial], obj[@key]) ) #
    loaded = load_helper(@key)
    @val = loaded[:new]
    @initial = loaded[:initial]
  else
    @val = obj[@key]
    @initial = clone_helper(obj[@key])
    tmp = {:initial => @initial, :new => @val}
    self.store_helper(@key, tmp)
  end
end

Public Instance Methods

clone_helper(o) click to toggle source
# File lib/durable.rb, line 63
def clone_helper(o)
  #marshalling used here to "deep clone" the object
  Marshal::load(Marshal::dump(o))
end
commit() click to toggle source
# File lib/durable.rb, line 35
def commit
  tmp = {:initial => @initial, :new => @val}
  self.store_helper(@key, tmp)
end
compare_helper(o1, o2) click to toggle source
# File lib/durable.rb, line 59
def compare_helper(o1, o2)
  return Marshal::dump(o1) == Marshal::dump(o2)
end
key_exists(key) click to toggle source
# File lib/durable.rb, line 55
def key_exists(key)
  File.exist? @@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s
end
load_helper(key) click to toggle source
# File lib/durable.rb, line 47
def load_helper(key)
  if(self.key_exists(key))
      Marshal::load(File.read(@@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s))
  else
    nil
  end
end
store_helper(key, val) click to toggle source
# File lib/durable.rb, line 40
def store_helper(key, val)
  Dir.mkdir(@@DURABLE_HOME) if !Dir.exists? @@DURABLE_HOME
  f = File.new(@@DURABLE_HOME+"/"+@@DURABLE_PREFIX+key.to_s, "w")
  f.print(Marshal::dump(val));
  f.close
end