Sha256: 2117530d45fe9cb704da223caa4f27b3a62c20b714692bf1533451b7ce7e6c82

Contents?: true

Size: 1.04 KB

Versions: 27

Compression:

Stored size: 1.04 KB

Contents

require 'json'
require 'pstore'
require 'fileutils'

# 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"}

class JSONStore < PStore

  def dump(table)
    table.to_json
  end

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


  # Dumps the whole store to hash
  # example:
  # store = JSONStore.new("my_file.json")
  # hash = store.transaction { store.to_h }
  def to_h
    @table
  end


  def marshal_dump_supports_canonical_option?
    false
  end

  EMPTY_MARSHAL_DATA = {}.to_json
  EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
  def empty_marshal_data
    EMPTY_MARSHAL_DATA
  end
  def empty_marshal_checksum
    EMPTY_MARSHAL_CHECKSUM
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
nutella_lib-0.6.0 lib/util/json_store.rb
nutella_lib-0.5.0 lib/util/json_store.rb
nutella_lib-0.4.27 lib/util/json_store.rb
nutella_lib-0.4.26 lib/util/json_store.rb
nutella_lib-0.4.25 lib/util/json_store.rb
nutella_lib-0.4.24 lib/util/json_store.rb
nutella_lib-0.4.23 lib/util/json_store.rb
nutella_lib-0.4.22 lib/util/json_store.rb
nutella_lib-0.4.21 lib/util/json_store.rb
nutella_lib-0.4.20 lib/util/json_store.rb
nutella_lib-0.4.19 lib/util/json_store.rb
nutella_lib-0.4.18 lib/util/json_store.rb
nutella_lib-0.4.17 lib/util/json_store.rb
nutella_lib-0.4.16 lib/util/json_store.rb
nutella_lib-0.4.15 lib/util/json_store.rb
nutella_lib-0.4.14 lib/util/json_store.rb
nutella_lib-0.4.13 lib/util/json_store.rb
nutella_lib-0.4.12 lib/util/json_store.rb
nutella_lib-0.4.11 lib/util/json_store.rb
nutella_lib-0.4.10 lib/util/json_store.rb