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.
-
#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.
410 411 412 |
# File 'lib/lotus/repository.rb', line 410 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>`.
509 510 511 |
# File 'lib/lotus/repository.rb', line 509 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 292 |
# File 'lib/lotus/repository.rb', line 286 def create(entity) unless entity.id @adapter.create(collection, entity) end entity 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.
386 387 388 389 390 391 392 393 394 |
# File 'lib/lotus/repository.rb', line 386 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.
433 434 435 |
# File 'lib/lotus/repository.rb', line 433 def find(id) @adapter.find(collection, id) end |
#first ⇒ Object?
Returns the first entity in the database.
462 463 464 |
# File 'lib/lotus/repository.rb', line 462 def first @adapter.first(collection) end |
#last ⇒ Object?
Returns the last entity in the database.
491 492 493 |
# File 'lib/lotus/repository.rb', line 491 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) @adapter.persist(collection, entity) entity 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.
336 337 338 339 340 341 342 343 344 |
# File 'lib/lotus/repository.rb', line 336 def update(entity) if entity.id @adapter.update(collection, entity) else raise Lotus::Model::NonPersistedEntityError end entity end |