Class: MemoryModel::Collection::Index::Unique

Inherits:
MemoryModel::Collection::Index show all
Defined in:
lib/memory_model/collection/index/unique.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.

Raises:



28
29
30
31
32
33
# File 'lib/memory_model/collection/index/unique.rb', line 28

def create(key, item)
  raise(NilValueError, self) if key.nil? && !options[:allow_nil]
  raise(RecordNotUniqueError, [self, key]) if index.has_key?(key)
  return if key.nil?
  index[key] = item
end

#delete(key) ⇒ Object

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



48
49
50
# File 'lib/memory_model/collection/index/unique.rb', line 48

def delete(key)
  index.delete_if { |k, value| key == value.uuid }
end

#exists?(item) ⇒ Boolean

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

Returns:

  • (Boolean)


53
54
55
# File 'lib/memory_model/collection/index/unique.rb', line 53

def exists?(item)
  index.any? { |key, value| item.uuid == value.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.



43
44
45
# File 'lib/memory_model/collection/index/unique.rb', line 43

def read(key)
  index[key]
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.



36
37
38
39
40
# File 'lib/memory_model/collection/index/unique.rb', line 36

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

#valuesObject

‘values` should return the values of the index



58
59
60
# File 'lib/memory_model/collection/index/unique.rb', line 58

def values
  index.values
end