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:



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

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:



69
70
71
72
73
74
# File 'lib/modules/persistent/store.rb', line 69

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



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

def delete!
  raise AbstractMethodCallError.new
end

#find(*ids) {|Object| ... } ⇒ nil, ...

Parameters:

  • ids (Array|String)

    or id to search

Yields:

  • (Object)

    for each object found

Returns:

  • (nil)

    if nothing found

  • (Object)

    if exactly one object found

  • (Array)

    of objects with matching ids if more than one matched



50
51
52
53
54
55
56
57
58
# File 'lib/modules/persistent/store.rb', line 50

def find(*ids)
  if block_given?
    all.select { |obj| ids.include?(obj.id) }.each do |obj|
      yield(obj)
    end
  else
    flatten all.select { |obj| ids.include?(obj.id) }
  end
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:



93
94
95
# File 'lib/modules/persistent/store.rb', line 93

def update(object)
  raise AbstractMethodCallError.new
end