Sha256: c02ac23e7bf4c870c9ddfb6f42258f013e1c4b8dfee75934e8cb2527575cdb67

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

require "key_vortex"
require "key_vortex/adapter"

class KeyVortex
  class Adapter
    # An in-memory implementation of an adapter. While typically not
    # useful in production, it can provide a convenient implementation
    # for unit testing.
    class Memory < KeyVortex::Adapter
      # A pass-through to the constructor which is used by
      # {KeyVortex.vortex}.
      # @param items [Hash]
      # @param limitations [Array of KeyVortex::Limitation]
      # @return [KeyVortex::Adapter::Memory]
      def self.build(items: {}, limitations: [])
        new(items, limitations: limitations)
      end

      # Create a new instance of the in-memory adapter. By default, it
      # has no limitations, but since it's most likely testing an
      # adapter which is more restrictive, it accepts a list of
      # limitations to apply.
      # @param items [Hash] The seed values that should be considered already saved.
      # @param limitations [Array of KeyVortex::Limitation]
      def initialize(items, limitations: [])
        super()
        @items = items
        limitations.each { |limit| register_limitation(limit) }
      end

      # Save the record to the provided hash using {Record#key} as the
      # key.
      # @param record [Record]
      def save(record)
        @items[record.key] = record
      end

      # @param key [String]
      # @return [Record, nil] The record with {Record#key} set to key
      def find(key)
        @items[key]
      end

      # Remove the record with {Record#key} set to key
      def remove(key)
        @items.delete(key)
      end
    end
  end
end

KeyVortex.register(KeyVortex::Adapter::Memory)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
key-vortex-1.1.1 lib/key_vortex/adapter/memory.rb
key-vortex-1.1.0 lib/key_vortex/adapter/memory.rb
key-vortex-1.0.0 lib/key_vortex/adapter/memory.rb