class RST::Persistent::Store
# The abstract Store-base-class
Store
provides the interface for all store-able classes
@abstract public API-methods should not be overwritten by descendant classes.
But descendants must overwrite all methods marked as abstract here.
Public Class Methods
Sets options-hash and calls the abstract ‘setup_backend’-callback
# File lib/modules/persistent/store.rb, line 21 def initialize(args={}) @options = {}.merge(args) setup_backend end
Public Instance Methods
Remove an object from the store @param [Persistentable] object - the object to be removed from store @return [Store] self
# File lib/modules/persistent/store.rb, line 35 def -(object) remove_object(object) self end
Add an object and sync store @param [Persistentable] object - any object including the Persistent-module
# File lib/modules/persistent/store.rb, line 28 def <<(object) update(object) end
@return Enumerable @abstract Overwrite in descendants thus it returns an Enumerable of all objects in the store
# File lib/modules/persistent/store.rb, line 80 def all raise AbstractMethodCallError.new end
Create objects and set the object’s store-attribute @example
new_object = store.create MyClass.new(....) end
@return [Persistentable] - the newly created object
# File lib/modules/persistent/store.rb, line 69 def create obj = yield obj.store = self self << obj obj end
Delete the store @abstract - override this method in descendants thus the store removes all objects.
# File lib/modules/persistent/store.rb, line 86 def delete! raise AbstractMethodCallError.new end
@param [Array|String] ids or id to search @yield [Object] for each object found @return [nil] if nothing found @return [Object] if exactly one object found @return [Array] of objects with matching ids if more than one matched
# 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
@return [Object|nil] the first object in the store or nil if the store is empty.
# File lib/modules/persistent/store.rb, line 41 def first all.first end
Find and update or add an object to the store @param [Object] object @abstract - override in other StoreClasses
# File lib/modules/persistent/store.rb, line 93 def update(object) raise AbstractMethodCallError.new end
Private Instance Methods
Flatten the result of a select @param [Array] result @return [Array] if result contains more than one element @return [Object] if result contains exactly one element @return [nil] if result is empty
# File lib/modules/persistent/store.rb, line 108 def flatten result if result.count == 1 result.first elsif result.nil? || result.empty? nil else result end end
@param [Object] object @abstract - override in concrete StoreClasses
# File lib/modules/persistent/store.rb, line 135 def remove_object(object) raise AbstractMethodCallError.new end
callback called from initializer. Aimed to initialize the objects-array, PStore, database-connection,… @abstract - Overwrite in descendants thus they initialize the store.
# File lib/modules/persistent/store.rb, line 123 def setup_backend raise AbstractMethodCallError.new end
Make sure the current state of objects is stored @abstract - Overwrite in descendants thus every object gets persistently stored.
# File lib/modules/persistent/store.rb, line 129 def sync_store raise AbstractMethodCallError.new end