Sha256: 509ac7de70a3267f4bff6449b16cfbc6aa95391f02841db3776f5efcce47716f

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

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

Version data entries

1 entries across 1 versions & 1 rubygems

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