Class: Picky::Backends::Memory::JSON

Inherits:
Basic show all
Defined in:
lib/picky/backends/memory/json.rb

Overview

Memory-based index files dumped in the JSON format.

Instance Attribute Summary

Attributes inherited from Basic

#cache_file_path, #hash_type

Instance Method Summary collapse

Methods inherited from Basic

#cache_path, #delete, #empty, #initial, #initialize, #to_s, #type

Methods included from Helpers::File

#create_directory

Constructor Details

This class inherits a constructor from Picky::Backends::Memory::Basic

Instance Method Details

#dump(internal) ⇒ Object

Dumps the index internal backend in json format.



33
34
35
36
# File 'lib/picky/backends/memory/json.rb', line 33

def dump internal
  create_directory cache_path
  dump_json internal
end

#dump_json(internal) ⇒ Object

Dump JSON into the cache file.

TODO Add IO option: MultiJson.encode(object, io: out_file)



43
44
45
46
47
48
49
50
51
# File 'lib/picky/backends/memory/json.rb', line 43

def dump_json internal
  ::File.open(cache_path, 'w') do |out_file|
    # If using Yajl, this will stream write to out_file.
    # Note: But it fails on oj.
    #
    # MultiJson.dump internal, [out_file]
    out_file.write MultiJson.encode internal
  end
end

#extensionObject

Uses the extension “json”.



17
18
19
# File 'lib/picky/backends/memory/json.rb', line 17

def extension
  :json
end

#load(symbol_keys) ⇒ Object

Loads the index hash from json format.

Also ensures all hash keys are frozen.



25
26
27
28
29
# File 'lib/picky/backends/memory/json.rb', line 25

def load symbol_keys
  MultiJson.decode ::File.open(cache_path, 'r'), symbolize_keys: symbol_keys # SYMBOLS.
  # index_hash && index_hash.each { |(key, value)| key.freeze }
  # index_hash
end

#retrieveObject

A json file does not provide retrieve functionality.



55
56
57
# File 'lib/picky/backends/memory/json.rb', line 55

def retrieve
  raise "Can't retrieve from JSON file. Use text file."
end