class Cuprum::Collections::Repository

A repository represents a group of collections.

Conceptually, a repository represents one or more underlying data stores. An application might have one repository for each data store, e.g. one repository for relational data, a second repository for document-based data, and so on. The application may instead aggregate all of its collections into a single repository, relying on the shared interface of all Collection implementations.

Public Class Methods

new() click to toggle source
# File lib/cuprum/collections/repository.rb, line 25
def initialize
  @collections = {}
end

Public Instance Methods

<<(collection)
Alias for: add
[](collection_name) click to toggle source

Finds and returns the collection with the given name.

@param collection_name [String, Symbol] The name of the collection to

return.

@return [Object] the requested collection.

@raise [Cuprum::Collection::Repository::UndefinedCOllectionError] if the

requested collection is not in the repository.
# File lib/cuprum/collections/repository.rb, line 45
def [](collection_name)
  @collections.fetch(collection_name.to_s) do
    raise UndefinedCollectionError,
      "repository does not define collection #{collection_name.inspect}"
  end
end
add(collection) click to toggle source

Adds the collection to the repository.

The collection must implement the collection_name property. Repository subclasses may enforce additional requirements.

@param collection [#collection_name] The collection to add to the

repository.

@return [Cuprum::Rails::Repository] the repository.

# File lib/cuprum/collections/repository.rb, line 61
def add(collection)
  validate_collection!(collection)

  @collections[collection.collection_name.to_s] = collection

  self
end
Also aliased as: <<
key?(collection_name) click to toggle source

Checks if a collection with the given name exists in the repository.

@param collection_name [String, Symbol] The name to check for.

@return [true, false] true if the key exists, otherwise false.

# File lib/cuprum/collections/repository.rb, line 75
def key?(collection_name)
  @collections.key?(collection_name.to_s)
end

Private Instance Methods

valid_collection?(collection) click to toggle source
# File lib/cuprum/collections/repository.rb, line 81
def valid_collection?(collection)
  collection.respond_to?(:collection_name)
end
validate_collection!(collection) click to toggle source
# File lib/cuprum/collections/repository.rb, line 85
def validate_collection!(collection)
  return if valid_collection?(collection)

  raise InvalidCollectionError,
    "#{collection.inspect} is not a valid collection"
end