Class: Pupa::Processor::DocumentStore::FileStore
- Inherits:
-
Object
- Object
- Pupa::Processor::DocumentStore::FileStore
- Defined in:
- lib/pupa/processor/document_store/file_store.rb
Overview
Stores JSON documents on disk.
Instance Method Summary collapse
-
#clear ⇒ Object
Deletes all files in the storage directory.
-
#delete(name) ⇒ Object
Delete a file with the given name.
-
#entries ⇒ Array<String>
Returns all file names in the storage directory.
-
#exist?(name) ⇒ Boolean
Returns whether a file with the given name exists.
-
#initialize(output_dir) ⇒ FileStore
constructor
A new instance of FileStore.
-
#path(name) ⇒ Object
Returns the path to the file with the given name.
-
#pipelined ⇒ Object
Collects commands to run all at once.
-
#read(name) ⇒ Hash
Returns, as JSON, the contents of the file with the given name.
-
#read_multi(names) ⇒ Array<Hash>
Returns, as JSON, the contents of the files with the given names.
-
#write(name, value) ⇒ Object
Writes, as JSON, the value to a file with the given name.
-
#write_multi(pairs) ⇒ Object
Writes, as JSON, the values to files with the given names.
-
#write_unless_exists(name, value) ⇒ Boolean
Writes, as JSON, the value to a file with the given name, unless such a file exists.
Constructor Details
#initialize(output_dir) ⇒ FileStore
Returns a new instance of FileStore.
11 12 13 14 |
# 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 |
Instance Method Details
#clear ⇒ Object
Deletes all files in the storage directory.
92 93 94 95 96 |
# 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(name) ⇒ Object
Delete a file with the given name.
87 88 89 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 87 def delete(name) File.delete(path(name)) end |
#entries ⇒ Array<String>
Returns all file names in the storage directory.
27 28 29 30 31 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 27 def entries Dir.chdir(@output_dir) do Dir['*.json'] end end |
#exist?(name) ⇒ Boolean
Returns whether a file with the given name exists.
20 21 22 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 20 def exist?(name) File.exist?(path(name)) end |
#path(name) ⇒ Object
Returns the path to the file with the given name.
107 108 109 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 107 def path(name) File.join(@output_dir, name) end |
#pipelined ⇒ Object
Collects commands to run all at once.
99 100 101 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 99 def pipelined yield end |
#read(name) ⇒ Hash
Returns, as JSON, the contents of the file with the given name.
37 38 39 40 41 |
# 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 |
#read_multi(names) ⇒ Array<Hash>
Returns, as JSON, the contents of the files with the given names.
47 48 49 50 51 |
# File 'lib/pupa/processor/document_store/file_store.rb', line 47 def read_multi(names) names.map do |name| read(name) end end |
#write(name, value) ⇒ Object
Writes, as JSON, the value to a file with the given name.
57 58 59 60 61 |
# 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 |
#write_multi(pairs) ⇒ Object
Writes, as JSON, the values to files with the given names.
78 79 80 81 82 |
# 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 |
#write_unless_exists(name, value) ⇒ Boolean
Writes, as JSON, the value to a file with the given name, unless such a file exists.
69 70 71 72 73 |
# 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 |