Class: RST::Persistent::Store Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/persistent/store.rb

Overview

This class is abstract.

public API-methods should not be overwritten by descendant classes. But descendants must overwrite all methods marked as abstract here.

# The abstract Store-base-class

Store provides the interface for all store-able classes

Direct Known Subclasses

DiskStore, MemoryStore

PUBLIC API collapse

ABSTRACT METHODS TO BE OVERWRITTEN IN DESCENDANTS collapse

Constructor Details

#initialize(args = {}) ⇒ Store

Sets options-hash and calls the abstract ‘setup_backend’-callback



21
22
23
24
# File 'lib/modules/persistent/store.rb', line 21

def initialize(args={})
  @options = {}.merge(args)
  setup_backend
end

Instance Method Details

#-(object) ⇒ Store

Remove an object from the store

Parameters:

Returns:



35
36
37
38
# File 'lib/modules/persistent/store.rb', line 35

def -(object)
  remove_object(object)
  self
end

#<<(object) ⇒ Object

Add an object and sync store

Parameters:

  • object (Persistentable)
    • any object including the Persistent-module



28
29
30
# File 'lib/modules/persistent/store.rb', line 28

def <<(object)
  update(object)
end

#allObject

This method is abstract.

Overwrite in descendants thus it returns an Enumerable of all objects in the store

Returns Enumerable.

Returns:

  • Enumerable

Raises:



73
74
75
# File 'lib/modules/persistent/store.rb', line 73

def all
  raise AbstractMethodCallError.new
end

#createPersistentable

Create objects and set the object’s store-attribute

Examples:


new_object = store.create
  MyClass.new(....)
end

Returns:



62
63
64
65
66
67
# File 'lib/modules/persistent/store.rb', line 62

def create
  obj = yield
  obj.store = self
  self << obj
  obj
end

#delete!Object

This method is abstract.
  • override this method in descendants thus the store removes all objects.

Delete the store



79
80
81
# File 'lib/modules/persistent/store.rb', line 79

def delete!
  raise AbstractMethodCallError.new
end

#find(*ids) ⇒ nil, ...

Parameters:

  • ids (Array|String)

    or id to search

Returns:

  • (nil)

    if nothing found

  • (Object)

    if exactly one object found

  • (Array)

    of objects with matching ids if more than one matched



49
50
51
# File 'lib/modules/persistent/store.rb', line 49

def find(*ids)
  flatten all.select { |obj| ids.include?(obj.id) }
end

#firstObject|nil

Returns the first object in the store or nil if the store is empty.

Returns:

  • (Object|nil)

    the first object in the store or nil if the store is empty.



41
42
43
# File 'lib/modules/persistent/store.rb', line 41

def first
  all.first
end

#update(object) ⇒ Object

This method is abstract.
  • override in other StoreClasses

Find and update or add an object to the store

Parameters:

  • object (Object)

Raises:



86
87
88
# File 'lib/modules/persistent/store.rb', line 86

def update(object)
  raise AbstractMethodCallError.new
end