Class: Cuprum::Collections::Repository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cuprum/collections/repository.rb

Overview

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.

Direct Known Subclasses

Basic::Repository

Defined Under Namespace

Classes: AbstractRepositoryError, DuplicateCollectionError, InvalidCollectionError, UndefinedCollectionError

Instance Method Summary collapse

Constructor Details

#initializeRepository

Returns a new instance of Repository.



32
33
34
# File 'lib/cuprum/collections/repository.rb', line 32

def initialize
  @collections = {}
end

Instance Method Details

#[](qualified_name) ⇒ Object

Finds and returns the collection with the given name.

Parameters:

  • qualified_name (String, Symbol)

    The qualified name of the collection to return.

Returns:

  • (Object)

    the requested collection.

Raises:

  • (Cuprum::Collection::Repository::UndefinedCOllectionError)

    if the requested collection is not in the repository.



52
53
54
55
56
57
# File 'lib/cuprum/collections/repository.rb', line 52

def [](qualified_name)
  @collections.fetch(qualified_name.to_s) do
    raise UndefinedCollectionError,
      "repository does not define collection #{qualified_name.inspect}"
  end
end

#add(collection, force: false) ⇒ Cuprum::Collections::Repository Also known as: <<

Adds the collection to the repository.

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

Parameters:

  • collection (Cuprum::Collections::Collection)

    the collection to add to the repository.

  • force (true, false) (defaults to: false)

    if true, override an existing collection with the same name.

Returns:

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cuprum/collections/repository.rb', line 73

def add(collection, force: false)
  validate_collection!(collection)

  if !force && key?(collection.qualified_name.to_s)
    raise DuplicateCollectionError,
      "collection #{collection.qualified_name} already exists"
  end

  @collections[collection.qualified_name.to_s] = collection

  self
end

#create(collection_name: nil, entity_class: nil, force: false, **options) ⇒ Cuprum::Collections::Collection

Adds a new collection with the given name to the repository.

Parameters:

  • collection_name (String) (defaults to: nil)

    the name of the new collection.

  • entity_class (Class, String) (defaults to: nil)

    the class of entity represented in the collection.

  • force (true, false) (defaults to: false)

    if true, override an existing collection with the same name.

  • options (Hash)

    additional options to pass to Collection.new.

Returns:

Raises:



101
102
103
104
105
106
107
# File 'lib/cuprum/collections/repository.rb', line 101

def create(force: false, **options)
  collection = build_collection(**options)

  add(collection, force: force)

  collection
end

#find_or_create(collection_name: nil, entity_class: nil, **options) ⇒ Cuprum::Collections::Collection

Finds or creates a new collection with the given name.

Parameters:

  • collection_name (String) (defaults to: nil)

    the name of the new collection.

  • entity_class (Class, String) (defaults to: nil)

    the class of entity represented in the collection.

  • options (Hash)

    additional options to pass to Collection.new.

Returns:

Raises:

  • (DuplicateCollectionError)

    if a collection with the same name but different parameters already exists in the repository.

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/cuprum/collections/repository.rb', line 121

def find_or_create(**parameters)
  qualified_name = qualified_name_for(**parameters)

  unless key?(qualified_name)
    create(**parameters)

    return @collections[qualified_name]
  end

  collection = @collections[qualified_name]

  return collection if collection.matches?(**parameters)

  raise DuplicateCollectionError,
    "collection #{qualified_name} already exists"
end

#key?(qualified_name) ⇒ true, false

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

Parameters:

  • qualified_name (String, Symbol)

    The name to check for.

Returns:

  • (true, false)

    true if the key exists, otherwise false.



143
144
145
# File 'lib/cuprum/collections/repository.rb', line 143

def key?(qualified_name)
  @collections.key?(qualified_name.to_s)
end

#keysArray<String>

Returns the names of the collections in the repository.

Returns:

  • (Array<String>)

    the collection names.



41
# File 'lib/cuprum/collections/repository.rb', line 41

def_delegators :@collections, :keys