Class: Pupa::Processor::DocumentStore::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/pupa/processor/document_store/file_store.rb

Overview

Stores JSON documents on disk.

See Also:

  • ActiveSupport::Cache::FileStore

Instance Method Summary collapse

Constructor Details

#initialize(output_dir) ⇒ FileStore

Returns a new instance of FileStore.

Parameters:

  • output_dir (String)

    the directory in which to dump JSON documents



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

#clearObject

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.

Parameters:



87
88
89
# File 'lib/pupa/processor/document_store/file_store.rb', line 87

def delete(name)
  File.delete(path(name))
end

#entriesArray<String>

Returns all file names in the storage directory.

Returns:

  • (Array<String>)

    all keys in the store



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.

Parameters:

Returns:

  • (Boolean)

    whether the store contains an entry for the given key



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.

Parameters:



107
108
109
# File 'lib/pupa/processor/document_store/file_store.rb', line 107

def path(name)
  File.join(@output_dir, name)
end

#pipelinedObject

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.

Parameters:

Returns:

  • (Hash)

    the value of the given key



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.

Parameters:

Returns:

  • (Array<Hash>)

    the values of the given keys



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.

Parameters:

  • name (String)

    a key

  • value (Hash)

    a value



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.

Parameters:

  • pairs (Hash)

    key-value pairs



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.

Parameters:

  • name (String)

    a key

  • value (Hash)

    a value

Returns:

  • (Boolean)

    whether the key was set



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