lib/nera/nera_database.rb in nera-0.1.2 vs lib/nera/nera_database.rb in nera-0.2.0
- old
+ new
@@ -1,9 +1,9 @@
require 'fileutils'
require 'yaml'
-#require 'filelock'
require 'pstore'
+require 'observer'
require 'pp'
#* list of public methods
#- initialize( path_to_filename )
#
@@ -71,19 +71,28 @@
# search a data file in this order
# classes of values
VALUE_CLASSES = [ String, Symbol, Integer, Float, Date, DateTime, TrueClass, FalseClass, NilClass ]
+ # notify to updates to observer
+ include Observable
+
public
# ----------------------------------
- def initialize( path_to_file )
+ def initialize( path_to_file, notify_observer_arg = nil )
@filename = path_to_file
@in_transaction = nil
+ @notify_observer_arg = notify_observer_arg
unless File.exist?(@filename)
raise "No such database : #{@filename}"
end
end
+
+ # ------------------------------------
+ def set_yaml_file( yml_path)
+ @yml_path = yml_path.to_s
+ end
# ------------------------------------
def transaction
if @in_transaction # nothing happens when the process is already in transaction
yield
@@ -92,10 +101,22 @@
@pstore = PStore.new(@filename)
@pstore.transaction( false) do
begin
@in_transaction = true
yield
+ if @notify_observer_arg
+ notify_observers( @notify_observer_arg)
+ else
+ notify_observers
+ end
+ if @yml_path
+ File.open( @yml_path,'w') do |io|
+ l = find_all do |r| true end
+ YAML.dump( l, io)
+ io.flush
+ end
+ end
ensure
@in_transaction = false
nil
end
end
@@ -138,10 +159,11 @@
transaction do
new_id = @pstore[:max_id] + 1
new_rec[:id] = new_id
@pstore[:max_id] = new_id
@pstore[new_id] = new_rec
+ changed
end
return new_id
end
private
@@ -233,10 +255,11 @@
unless @pstore.root?( id_to_update)
return nil
end
@pstore[id_to_update] = rec
flag = true
+ changed
end
return flag
end
public
@@ -265,9 +288,10 @@
return flag
elsif ids.is_a?( Integer)
stat = nil
transaction do
stat = @pstore.delete(ids)
+ changed
end
return stat
else
raise ArgumentError, "argument must be specified by Integer, Array of Integer, or Range. : #{ids.class}"
end