lib/ruote/storage/fs_storage.rb in ruote-2.2.0 vs lib/ruote/storage/fs_storage.rb in ruote-2.3.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2012, 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
@@ -20,23 +20,14 @@
# THE SOFTWARE.
#
# Made in Japan.
#++
-begin
- require 'yajl'
-rescue LoadError
- require 'json'
-end
- # gem install yajl-ruby OR json OR json_pure OR json-jruby
-require 'rufus/json'
-Rufus::Json.detect_backend
+require 'rufus-json/automatic'
+require 'rufus-cloche'
-require 'rufus/cloche'
- # gem install rufus-cloche
-
require 'ruote/storage/base'
module Ruote
@@ -57,23 +48,35 @@
# The options are classical engine configuration, but the 'cloche_nolock'
# option is read by the storage and followed.
#
def initialize(dir, options={})
+ if dir.is_a?(Hash) && options == {}
+ options = dir
+ dir = options.delete('dir')
+ end
+
FileUtils.mkdir_p(dir)
@cloche = Rufus::Cloche.new(
:dir => dir, :nolock => options['cloche_nolock'])
- @options = options
+ replace_engine_configuration(options)
+ end
- @cloche.put(@options.merge('type' => 'configurations', '_id' => 'engine'))
+ def dir
+
+ @cloche.dir
end
def put(doc, opts={})
- @cloche.put(doc.merge!('put_at' => Ruote.now_to_utc_s), opts)
+ doc = doc.send(
+ opts[:update_rev] ? 'merge!' : 'merge',
+ 'put_at' => Ruote.now_to_utc_s)
+
+ @cloche.put(doc, opts)
end
def get(type, key)
@cloche.get(type, key)
@@ -84,17 +87,17 @@
@cloche.delete(doc)
end
def get_many(type, key=nil, opts={})
- if key
- key = Array(key)
- key = key.map { |k| "!#{k}" } if key.first.is_a?(String)
- end
- # assuming /!#{wfid}$/...
+ keys = key ? Array(key) : nil
- @cloche.get_many(type, key, opts)
+ keys = keys.map { |k|
+ type == 'schedules' ? /!#{k}-\d+$/ : "!#{k}"
+ } if keys && keys.first.is_a?(String)
+
+ @cloche.get_many(type, keys, opts)
end
def ids(type)
@cloche.ids(type)
@@ -105,29 +108,17 @@
def purge!
FileUtils.rm_rf(@cloche.dir)
end
- # No need for that here (FsStorage adds type on the fly).
+ # No need for that here (FsStorage can add types on the fly).
#
def add_type(type)
end
def purge_type!(type)
@cloche.purge_type!(type)
- end
-
- def dump(type)
-
- s = "=== #{type} ===\n"
-
- @cloche.get_many(type).inject(s) do |s1, e|
- s1 << "\n"
- e.keys.sort.inject(s1) do |s2, k|
- s2 << " #{k} => #{e[k].inspect}\n"
- end
- end
end
# Shuts this storage down.
#
def shutdown