Module: Epiphy::Repository::ClassMethods
- Includes:
- Helper
- Defined in:
- lib/epiphy/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.
-
#count ⇒ Object
Count the entity in this 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
Finds an entity by its identity.
-
#first(order_by = :id) ⇒ Object?
Returns the first entity in the database.
-
#last(order_by = :id) ⇒ 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.
Methods included from Helper
Instance Method Details
#adapter=(adapter) ⇒ Object
Assigns an adapter.
Epiphy::Repository is shipped with an adapters:
* Rethinkdb
244 245 246 |
# File 'lib/epiphy/repository.rb', line 244 def adapter=(adapter) @adapter = adapter end |
#all ⇒ Array<Object>
Returns all the persisted entities.
446 447 448 449 450 451 452 |
# File 'lib/epiphy/repository.rb', line 446 def all all_row = @adapter.all(collection) cursor = Epiphy::Repository::Cursor.new all_row do |item| to_entity(item) end cursor.to_a end |
#clear ⇒ Object
Deletes all the records from the current collection.
Execute a ‘r.table().delete()` on RethinkDB level.
572 573 574 |
# File 'lib/epiphy/repository.rb', line 572 def clear @adapter.clear(collection) end |
#count ⇒ Object
Count the entity in this collection
582 583 584 |
# File 'lib/epiphy/repository.rb', line 582 def count @adapter.count(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 will try to insert use that id and will raise an error if the id is already exist
319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/epiphy/repository.rb', line 319 def create(entity) #unless entity.id begin result = @adapter.create(collection, to_document(entity)) entity.id = result rescue Epiphy::Model::EntityExisted => e raise e rescue RethinkDB::RqlRuntimeError => e raise Epiphy::Model::RuntimeError, e. end #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.
422 423 424 425 426 427 428 429 430 |
# File 'lib/epiphy/repository.rb', line 422 def delete(entity) if entity.id @adapter.delete(collection, entity.id) else raise Epiphy::Model::NonPersistedEntityError end entity end |
#find(id) ⇒ Object
Finds an entity by its identity.
If used with a SQL database, it corresponds to the primary key.
476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/epiphy/repository.rb', line 476 def find(id) entity_id = id if !id.is_a? String raise Epiphy::Model::EntityIdNotFound, "Missing entity id" if !id.respond_to?(:to_s) entity_id = id.to_s end #if !id.is_a? String #entity_id = id.to_i #end result = @adapter.find(collection, entity_id).tap do |record| raise Epiphy::Model::EntityNotFound.new unless record end to_entity(result) end |
#first(order_by = :id) ⇒ Object?
Returns the first entity in the database.
516 517 518 519 520 521 522 523 |
# File 'lib/epiphy/repository.rb', line 516 def first(order_by=:id) result = @adapter.first(collection, order_by: order_by) if result to_entity result else result end end |
#last(order_by = :id) ⇒ Object?
Returns the last entity in the database.
550 551 552 553 554 555 556 |
# File 'lib/epiphy/repository.rb', line 550 def last(order_by=:id) if result = @adapter.last(collection, order_by: order_by) to_entity result else nil end end |
#persist(entity) ⇒ Object
Creates or updates a record in the database for the given entity.
287 288 289 |
# File 'lib/epiphy/repository.rb', line 287 def persist(entity) @adapter.persist(collection, to_document(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.
374 375 376 377 378 379 380 |
# File 'lib/epiphy/repository.rb', line 374 def update(entity) if entity.id @adapter.update(collection, to_document(entity)) else raise Epiphy::Model::NonPersistedEntityError end end |