Class: JSONStore

Inherits:
PStore
  • Object
show all
Defined in:
lib/util/json_store.rb

Overview

JSONStore provides the same functionality as PStore, except it uses JSON to dump objects instead of Marshal. Example use:

store = JSONStore.new("json_store/json_test.json")
# Write
store.transaction { store["key"]="value" }
# Read
value = store.transaction { store["key"] }
puts value # prints "value"
# Dump the whole store
hash = store.transaction { store.to_h }
p hash # prints {"key" => "value"}

Constant Summary collapse

EMPTY_MARSHAL_DATA =
{}.to_json
EMPTY_MARSHAL_CHECKSUM =
Digest::MD5.digest(EMPTY_MARSHAL_DATA)

Instance Method Summary collapse

Instance Method Details

#dump(table) ⇒ Object



20
21
22
# File 'lib/util/json_store.rb', line 20

def dump(table)
  table.to_json
end

#empty_marshal_checksumObject



47
48
49
# File 'lib/util/json_store.rb', line 47

def empty_marshal_checksum
  EMPTY_MARSHAL_CHECKSUM
end

#empty_marshal_dataObject



44
45
46
# File 'lib/util/json_store.rb', line 44

def empty_marshal_data
  EMPTY_MARSHAL_DATA
end

#load(content) ⇒ Object



24
25
26
# File 'lib/util/json_store.rb', line 24

def load(content)
  JSON.parse(content)
end

#marshal_dump_supports_canonical_option?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/util/json_store.rb', line 38

def marshal_dump_supports_canonical_option?
  false
end

#to_hObject

Dumps the whole store to hash example: store = JSONStore.new(“my_file.json”) hash = store.transaction { store.to_h }



33
34
35
# File 'lib/util/json_store.rb', line 33

def to_h
  @table
end