lib/ruote/storage/hash_storage.rb in ruote-2.1.11 vs lib/ruote/storage/hash_storage.rb in ruote-2.2.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -37,21 +37,23 @@ include StorageBase include MonitorMixin attr_reader :h - def initialize (options={}) + def initialize(options={}) super() # since were including MonitorMixin, this super() is necessary + @options = options + purge! put(options.merge('type' => 'configurations', '_id' => 'engine')) end - def put (doc, opts={}) + def put(doc, opts={}) i = @h.size synchronize do @@ -77,20 +79,27 @@ @h[doc['type']][doc['_id']] = Rufus::Json.dup(doc) nil end + + rescue => e + puts "=" * 80 + File.open('doc.json', 'wb') do |f| + f.puts Rufus::Json.pretty_encode(doc) + end + raise e end - def get (type, key) + def get(type, key) synchronize do Ruote.fulldup(@h[type][key]) end end - def delete (doc) + def delete(doc) drev = doc['_rev'] raise ArgumentError.new("can't delete doc without _rev") unless drev @@ -100,24 +109,19 @@ return true if prev.nil? doc['_rev'] ||= 0 - if prev['_rev'] == drev + return prev if prev['_rev'] != drev - @h[doc['type']].delete(doc['_id']) + @h[doc['type']].delete(doc['_id']) - nil - - else - - prev - end + nil # success end end - def get_many (type, key=nil, opts={}) + def get_many(type, key=nil, opts={}) # NOTE : no dup here for now synchronize do @@ -143,11 +147,11 @@ end end # Returns a sorted list of all the ids for a given type. # - def ids (type) + def ids(type) @h[type].keys.sort end # Purges the storage completely. @@ -171,30 +175,37 @@ } @h['configurations']['engine'] = @options end - def add_type (type) + def add_type(type) @h[type] = {} end - def purge_type! (type) + def purge_type!(type) @h[type] = {} end - def dump (type) + def dump(type) s = "=== #{type} ===\n" @h[type].inject(s) do |s1, (k, v)| s1 << "\n" s1 << "#{k} :\n" v.keys.sort.inject(s1) do |s2, k1| s2 << " #{k1} => #{v[k1].inspect}\n" end end + end + + # Shuts this storage down. + # + def shutdown + + # nothing to do end end end