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.
408 409 410 |
# File 'lib/lotus/repository.rb', line 408 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>`.
507 508 509 |
# File 'lib/lotus/repository.rb', line 507 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.
286 287 288 289 290 291 |
# File 'lib/lotus/repository.rb', line 286 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.
384 385 386 387 388 389 390 391 392 |
# File 'lib/lotus/repository.rb', line 384 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.
431 432 433 |
# File 'lib/lotus/repository.rb', line 431 def find(id) @adapter.find(collection, id) end |
#first ⇒ Object?
Returns the first entity in the database.
460 461 462 |
# File 'lib/lotus/repository.rb', line 460 def first @adapter.first(collection) end |
#last ⇒ Object?
Returns the last entity in the database.
489 490 491 |
# File 'lib/lotus/repository.rb', line 489 def last @adapter.last(collection) end |
#persist(entity) ⇒ Object
Creates or updates a record in the database for the given entity.
254 255 256 257 |
# File 'lib/lotus/repository.rb', line 254 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.
548 549 550 551 552 |
# File 'lib/lotus/repository.rb', line 548 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.
335 336 337 338 339 340 341 342 |
# File 'lib/lotus/repository.rb', line 335 def update(entity) if _persisted?(entity) _touch(entity) @adapter.update(collection, entity) else raise Lotus::Model::NonPersistedEntityError end end |