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

#initialize {|the| ... } ⇒ Repository

Returns a new instance of Repository.

Yield Parameters:



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.

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.



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.

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:



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, frozen_error_message 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

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

Parameters:

  • 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:



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)

  error_message = partial_match_error_message(collection:, parameters:)

  raise DuplicateCollectionError, error_message
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:



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.

Parameters:

  • qualified_name (String, Symbol)

    The name to check for.

Returns:

  • (true, false)

    true if the key exists, otherwise false.



170
171
172
# File 'lib/cuprum/collections/repository.rb', line 170

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.



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.

Parameters:

  • qualified_name (String, Symbol)

    the name of the collection to remove.

Returns:

Raises:

  • (FrozenError)


180
181
182
183
184
185
186
187
188
# File 'lib/cuprum/collections/repository.rb', line 180

def remove(qualified_name:)
  raise FrozenError, frozen_error_message if frozen?

  collection = find(qualified_name:)

  @collections.delete(collection.qualified_name)

  collection
end