class Pupa::Processor::DocumentStore::FileStore
Stores JSON documents on disk.
@see ActiveSupport::Cache::FileStore
Public Class Methods
@param [String] output_dir the directory in which to dump JSON documents
# File lib/pupa/processor/document_store/file_store.rb, line 11 def initialize(output_dir) @output_dir = output_dir FileUtils.mkdir_p(@output_dir) end
Public Instance Methods
Deletes all files in the storage directory.
# File lib/pupa/processor/document_store/file_store.rb, line 92 def clear Dir[File.join(@output_dir, '*.json')].each do |path| File.delete(path) end end
Delete a file with the given name.
@param [String] name a key
# File lib/pupa/processor/document_store/file_store.rb, line 87 def delete(name) File.delete(path(name)) end
Returns all file names in the storage directory.
@return [Array<String>] all keys in the store
# File lib/pupa/processor/document_store/file_store.rb, line 27 def entries Dir.chdir(@output_dir) do Dir['*.json'] end end
Returns whether a file with the given name exists.
@param [String] name a key @return [Boolean] whether the store contains an entry for the given key
# File lib/pupa/processor/document_store/file_store.rb, line 20 def exist?(name) File.exist?(path(name)) end
Returns the path to the file with the given name.
@param [String] name a key @param [String] a path
# File lib/pupa/processor/document_store/file_store.rb, line 107 def path(name) File.join(@output_dir, name) end
Collects commands to run all at once.
# File lib/pupa/processor/document_store/file_store.rb, line 99 def pipelined yield end
Returns, as JSON, the contents of the file with the given name.
@param [String] name a key @return [Hash] the value of the given key
# File lib/pupa/processor/document_store/file_store.rb, line 37 def read(name) File.open(path(name)) do |f| Oj.load(f) end end
Returns, as JSON, the contents of the files with the given names.
@param [String] names keys @return [Array<Hash>] the values of the given keys
# File lib/pupa/processor/document_store/file_store.rb, line 47 def read_multi(names) names.map do |name| read(name) end end
Writes, as JSON, the value to a file with the given name.
@param [String] name a key @param [Hash] value a value
# File lib/pupa/processor/document_store/file_store.rb, line 57 def write(name, value) File.open(path(name), 'w') do |f| f.write(Oj.dump(value, mode: :compat, time_format: :ruby)) end end
Writes, as JSON, the values to files with the given names.
@param [Hash] pairs key-value pairs
# File lib/pupa/processor/document_store/file_store.rb, line 78 def write_multi(pairs) pairs.each do |name,value| write(name, 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
# File lib/pupa/processor/document_store/file_store.rb, line 69 def write_unless_exists(name, value) !exist?(name).tap do |exists| write(name, value) unless exists end end