Module: Lotus::Repository::ClassMethods
- Defined in:
- lib/lotus/repository.rb
Overview
Instance Method Summary collapse
- #adapter ⇒ Object private
-
#adapter=(adapter) ⇒ Object
Assigns an adapter.
-
#all ⇒ Array<Object>
Returns all the persisted entities.
-
#clear ⇒ Object
Deletes all the records from the current collection.
-
#create(entity) ⇒ Object
Creates a record in the database for the given entity.
-
#delete(entity) ⇒ Object
Deletes a record in the database corresponding to the given entity.
-
#find(id) ⇒ Object, NilClass
Finds an entity by its identity.
-
#first ⇒ Object?
Returns the first entity in the database.
-
#last ⇒ Object?
Returns the last entity in the database.
-
#persist(entity) ⇒ Object
Creates or updates a record in the database for the given entity.
-
#transaction(options = {}) ⇒ Object
Wraps the given block in a transaction.
-
#update(entity) ⇒ Object
Updates a record in the database corresponding to the given entity.
Instance Method Details
#adapter ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
217 218 219 |
# File 'lib/lotus/repository.rb', line 217 def adapter @adapter end |
#adapter=(adapter) ⇒ Object
Assigns an adapter.
Lotus::Model is shipped with two adapters:
* SqlAdapter
* MemoryAdapter
211 212 213 |
# File 'lib/lotus/repository.rb', line 211 def adapter=(adapter) @adapter = adapter end |
#all ⇒ Array<Object>
Returns all the persisted entities.
421 422 423 |
# File 'lib/lotus/repository.rb', line 421 def all @adapter.all(collection) end |
#clear ⇒ Object
Deletes all the records from the current collection.
If used with a SQL database it executes a ‘DELETE FROM <table>`.
520 521 522 |
# File 'lib/lotus/repository.rb', line 520 def clear @adapter.clear(collection) end |
#create(entity) ⇒ Object
Creates a record in the database for the given entity. It returns a copy of the entity with ‘id` assigned.
If already persisted (‘id` present) it does nothing.
299 300 301 302 303 304 |
# File 'lib/lotus/repository.rb', line 299 def create(entity) unless _persisted?(entity) _touch(entity) @adapter.create(collection, entity) end end |
#delete(entity) ⇒ Object
Deletes a record in the database corresponding to the given entity.
If not already persisted (‘id` present) it raises an exception.
397 398 399 400 401 402 403 404 405 |
# File 'lib/lotus/repository.rb', line 397 def delete(entity) if _persisted?(entity) @adapter.delete(collection, entity) else raise Lotus::Model::NonPersistedEntityError end entity end |
#find(id) ⇒ Object, NilClass
Finds an entity by its identity.
If used with a SQL database, it corresponds to the primary key.
444 445 446 |
# File 'lib/lotus/repository.rb', line 444 def find(id) @adapter.find(collection, id) end |
#first ⇒ Object?
Returns the first entity in the database.
473 474 475 |
# File 'lib/lotus/repository.rb', line 473 def first @adapter.first(collection) end |
#last ⇒ Object?
Returns the last entity in the database.
502 503 504 |
# File 'lib/lotus/repository.rb', line 502 def last @adapter.last(collection) end |
#persist(entity) ⇒ Object
Creates or updates a record in the database for the given entity.
261 262 263 264 |
# File 'lib/lotus/repository.rb', line 261 def persist(entity) _touch(entity) @adapter.persist(collection, entity) end |
#transaction(options = {}) ⇒ Object
Wraps the given block in a transaction.
For performance reasons the block isn’t in the signature of the method, but it’s yielded at the lower level.
Please note that it’s only supported by some databases. For this reason, the accepted options may be different from adapter to adapter.
For advanced scenarios, please check the documentation of each adapter.
561 562 563 564 565 |
# File 'lib/lotus/repository.rb', line 561 def transaction( = {}) @adapter.transaction() do yield end end |
#update(entity) ⇒ Object
Updates a record in the database corresponding to the given entity.
If not already persisted (‘id` present) it raises an exception.
348 349 350 351 352 353 354 355 |
# File 'lib/lotus/repository.rb', line 348 def update(entity) if _persisted?(entity) _touch(entity) @adapter.update(collection, entity) else raise Lotus::Model::NonPersistedEntityError end end |