Class: KeyVortex::Adapter::Memory

Inherits:
KeyVortex::Adapter show all
Defined in:
lib/key_vortex/adapter/memory.rb

Overview

An in-memory implementation of an adapter. While typically not useful in production, it can provide a convenient implementation for unit testing.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KeyVortex::Adapter

#limitation_for, #register_limitation, symbol

Constructor Details

#initialize(items, limitations: []) ⇒ Memory

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.

Parameters:

  • items (Hash)

    The seed values that should be considered already saved.

  • limitations (Array of KeyVortex::Limitation) (defaults to: [])


27
28
29
30
31
# File 'lib/key_vortex/adapter/memory.rb', line 27

def initialize(items, limitations: [])
  super()
  @items = items
  limitations.each { |limit| register_limitation(limit) }
end

Class Method Details

.build(items: {}, limitations: []) ⇒ KeyVortex::Adapter::Memory

A pass-through to the constructor which is used by KeyVortex.vortex.

Parameters:

Returns:



17
18
19
# File 'lib/key_vortex/adapter/memory.rb', line 17

def self.build(items: {}, limitations: [])
  new(items, limitations: limitations)
end

Instance Method Details

#find(key) ⇒ Record?

Returns The record with Record#key set to key.

Parameters:

  • key (String)

Returns:



42
43
44
# File 'lib/key_vortex/adapter/memory.rb', line 42

def find(key)
  @items[key]
end

#remove(key) ⇒ Object

Remove the record with Record#key set to key



47
48
49
# File 'lib/key_vortex/adapter/memory.rb', line 47

def remove(key)
  @items.delete(key)
end

#save(record) ⇒ Object

Save the record to the provided hash using Record#key as the key.

Parameters:



36
37
38
# File 'lib/key_vortex/adapter/memory.rb', line 36

def save(record)
  @items[record.key] = record
end