require "kvmultiplex/provider" require "multi_json" # because Chef uses json 1.8.old-as-heck require "deep_merge" module KVMultiplex module Providers # The `Memory` provider just stores a list of potential values. Since other # providers are sparse trees (there's no "list" operation), we're just going # to store values as complete subkeys, stringified internally to avoid # accidentally changing a subkey further down the line. class Memory < KVMultiplex::Provider def initialize @contents = {} end def get(subkey, _full_key) v = @contents[get_content_key(subkey)] v.nil? ? v : MultiJson.load(v) end def set(subkey, _full_key, value) # do NOT use JSON.generate here; it's unsafe in environments that pull json 1.8.6 @contents[get_content_key(subkey)] = value.nil? ? nil : MultiJson.dump(value) value end private def get_content_key(subkey) # Was originally a SHA1 hash, but I think that might be overkill. subkey.join('.') end end end end