lib/pupa/processor/document_store/file_store.rb in pupa-0.0.8 vs lib/pupa/processor/document_store/file_store.rb in pupa-0.0.9

- old
+ new

@@ -32,11 +32,11 @@ # # @param [String] name a key # @return [Hash] the value of the given key def read(name) File.open(namespaced_key(name)) do |f| - JSON.load(f) + MultiJson.load(f) end end # Returns, as JSON, the contents of the files with the given names. # @@ -52,14 +52,35 @@ # # @param [String] name a key # @param [Hash] value a value def write(name, value) File.open(namespaced_key(name), 'w') do |f| - JSON.dump(value, f) + f.write(MultiJson.dump(value)) end end + # Writes, as JSON, the value to a file with the given name, unless such + # a file exists. + # + # @param [String] name a key + # @param [Hash] value a value + # @return [Boolean] whether the key was set + def write_unless_exists(name, value) + !exist?(name).tap do |exists| + write(name, value) unless exists + end + end + + # Writes, as JSON, the values to files with the given names. + # + # @param [Hash] pairs key-value pairs + def write_multi(pairs) + pairs.each do |name,value| + write(name, value) + end + end + # Delete a file with the given name. # # @param [String] name a key def delete(name) File.delete(namespaced_key(name)) @@ -68,9 +89,14 @@ # Deletes all files in the storage directory. def clear Dir[File.join(@output_dir, '*.json')].each do |path| File.delete(path) end + end + + # Collects commands to run all at once. + def pipelined + yield end private def namespaced_key(name)