Module: Lotus::Repository::ClassMethods
- Defined in:
- lib/lotus/repository.rb
Overview
Instance Method Summary collapse
-
#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=(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.
405 406 407 |
# File 'lib/lotus/repository.rb', line 405 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>`.
504 505 506 |
# File 'lib/lotus/repository.rb', line 504 def clear @adapter.clear(collection) end |
#create(entity) ⇒ Object
Creates a record in the database for the given entity. It assigns the id attribute, in case of success.
If already persisted (id present) it does nothing.
285 286 287 288 289 |
# File 'lib/lotus/repository.rb', line 285 def create(entity) unless entity.id @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.
381 382 383 384 385 386 387 388 389 |
# File 'lib/lotus/repository.rb', line 381 def delete(entity) if entity.id @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.
428 429 430 |
# File 'lib/lotus/repository.rb', line 428 def find(id) @adapter.find(collection, id) end |
#first ⇒ Object?
Returns the first entity in the database.
457 458 459 |
# File 'lib/lotus/repository.rb', line 457 def first @adapter.first(collection) end |
#last ⇒ Object?
Returns the last entity in the database.
486 487 488 |
# File 'lib/lotus/repository.rb', line 486 def last @adapter.last(collection) end |
#persist(entity) ⇒ Object
Creates or updates a record in the database for the given entity.
254 255 256 |
# File 'lib/lotus/repository.rb', line 254 def persist(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.
545 546 547 548 549 |
# File 'lib/lotus/repository.rb', line 545 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.
333 334 335 336 337 338 339 |
# File 'lib/lotus/repository.rb', line 333 def update(entity) if entity.id @adapter.update(collection, entity) else raise Lotus::Model::NonPersistedEntityError end end |