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(name: nil, entity_class: nil, force: false, **options) ⇒ Cuprum::Collections::Collection
Adds a new collection with the given name to the repository.
- #find(name: nil, entity_class: nil, **options) ⇒ Object
-
#find_or_create(collection_name: nil, entity_class: nil, **options) ⇒ Cuprum::Collections::Collection
Finds or creates a new collection with the given name.
-
#initialize {|the| ... } ⇒ 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.
-
#remove(qualified_name:) ⇒ Cuprum::Collections::Collection
Removes the specified collection from the repository.
Constructor Details
#initialize {|the| ... } ⇒ Repository
Returns a new instance of Repository.
33 34 35 36 37 |
# File 'lib/cuprum/collections/repository.rb', line 33 def initialize @collections = {} yield self if block_given? end |
Instance Method Details
#[](qualified_name) ⇒ Object
Finds and returns the collection with the given name.
55 56 57 58 59 60 |
# File 'lib/cuprum/collections/repository.rb', line 55 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.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cuprum/collections/repository.rb', line 76 def add(collection, force: false) validate_collection!(collection) raise FrozenError, if frozen? 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(name: nil, entity_class: nil, force: false, **options) ⇒ Cuprum::Collections::Collection
106 107 108 109 110 111 112 |
# File 'lib/cuprum/collections/repository.rb', line 106 def create(force: false, **) collection = build_collection(**) add(collection, force:) collection end |
#find(name: nil, entity_class: nil, **options) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cuprum/collections/repository.rb', line 115 def find(**parameters) qualified_name = qualified_name_for(**parameters) collection = @collections[qualified_name] unless collection raise UndefinedCollectionError, "repository does not define collection #{qualified_name.inspect}" end return collection if collection.matches?(**parameters) = (collection:, parameters:) raise DuplicateCollectionError, end |
#find_or_create(collection_name: nil, entity_class: nil, **options) ⇒ Cuprum::Collections::Collection
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/cuprum/collections/repository.rb', line 143 def find_or_create(**parameters) # rubocop:disable Metrics/MethodLength tools.core_tools.deprecate( "#{self.class.name}#find_or_create()", message: 'Use #create or #find method.' ) 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.
170 171 172 |
# File 'lib/cuprum/collections/repository.rb', line 170 def key?(qualified_name) @collections.key?(qualified_name.to_s) end |
#keys ⇒ Array<String>
Returns the names of the collections in the repository.
44 |
# File 'lib/cuprum/collections/repository.rb', line 44 def_delegators :@collections, :keys |
#remove(qualified_name:) ⇒ Cuprum::Collections::Collection
Removes the specified collection from the repository.
180 181 182 183 184 185 186 187 188 |
# File 'lib/cuprum/collections/repository.rb', line 180 def remove(qualified_name:) raise FrozenError, if frozen? collection = find(qualified_name:) @collections.delete(collection.qualified_name) collection end |