Sha256: 8bc0ae9ea099f765f3712b53904f29132e5dd941198137a8ebae382962f62930

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require "kvmultiplex/provider"

require "json"
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 : JSON.parse(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 : value.to_json
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kvmultiplex-0.1.7 lib/kvmultiplex/providers/memory.rb