Class: Cuprum::Collections::Repository
- Inherits:
-
Object
- Object
- Cuprum::Collections::Repository
- 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
Defined Under Namespace
Classes: AbstractRepositoryError, DuplicateCollectionError, InvalidCollectionError, UndefinedCollectionError
Instance Method Summary collapse
-
#[](qualified_name) ⇒ Object
Finds and returns the collection with the given name.
-
#add(collection, force: false) ⇒ Cuprum::Collections::Repository
(also: #<<)
Adds the collection to the repository.
-
#create(collection_name: nil, entity_class: nil, force: false, **options) ⇒ Cuprum::Collections::Collection
Adds a new collection with the given name to the repository.
-
#find_or_create(collection_name: nil, entity_class: nil, **options) ⇒ Cuprum::Collections::Collection
Finds or creates a new collection with the given name.
-
#initialize ⇒ Repository
constructor
A new instance of Repository.
-
#key?(qualified_name) ⇒ true, false
Checks if a collection with the given name exists in the repository.
-
#keys ⇒ Array<String>
Returns the names of the collections in the repository.
Constructor Details
#initialize ⇒ Repository
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.
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.
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
101 102 103 104 105 106 107 |
# File 'lib/cuprum/collections/repository.rb', line 101 def create(force: false, **) collection = build_collection(**) add(collection, force: force) collection end |
#find_or_create(collection_name: nil, entity_class: nil, **options) ⇒ Cuprum::Collections::Collection
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.
143 144 145 |
# File 'lib/cuprum/collections/repository.rb', line 143 def key?(qualified_name) @collections.key?(qualified_name.to_s) end |
#keys ⇒ Array<String>
Returns the names of the collections in the repository.
41 |
# File 'lib/cuprum/collections/repository.rb', line 41 def_delegators :@collections, :keys |