Class: MemoryModel::Collection::Index::Multi

Inherits:
MemoryModel::Collection::Index show all
Defined in:
lib/memory_model/collection/index/multi.rb

Instance Attribute Summary

Attributes inherited from MemoryModel::Collection::Index

#index, #name, #options

Instance Method Summary collapse

Methods inherited from MemoryModel::Collection::Index

#initialize, #where

Constructor Details

This class inherits a constructor from MemoryModel::Collection::Index

Instance Method Details

#create(key, item) ⇒ Object

‘create` should implement creating a new record, raising an error if an item with the matching storage id already exists in the index.



8
9
10
# File 'lib/memory_model/collection/index/multi.rb', line 8

def create(key, item)
  insert_into_key(key, item)
end

#delete(key) ⇒ Object

‘delete` should find a record in the collection by its indexed_value, and remove it.



25
26
27
# File 'lib/memory_model/collection/index/multi.rb', line 25

def delete(key)
  index.values.map { |refs| refs.delete key }
end

#exists?(item) ⇒ Boolean

‘exists?` return whether or not an item with the given storage id exists.

Returns:

  • (Boolean)


30
31
32
# File 'lib/memory_model/collection/index/multi.rb', line 30

def exists?(item)
  index.values.any? { |refs| refs.has_key? item.uuid }
end

#read(key) ⇒ Object

‘read` should find a record in the collection by its indexed_value, remove it, and add with the new value.



20
21
22
# File 'lib/memory_model/collection/index/multi.rb', line 20

def read(key)
  index[key].first
end

#update(key, item) ⇒ Object

‘update` should find a record in the collection by its storage_id, remove it, and add with the new value.



13
14
15
16
17
# File 'lib/memory_model/collection/index/multi.rb', line 13

def update(key, item)
  raise(RecordNotInIndexError, [self, item]) unless exists? item
  delete(item)
  create(key, item)
end

#valuesObject

‘values` should return the values of the index



35
36
37
# File 'lib/memory_model/collection/index/multi.rb', line 35

def values
  index.values.map(&:values)
end