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
261 262 263 |
# File 'lib/epiphy/repository.rb', line 261 def adapter=(adapter) @adapter = adapter end |
#all ⇒ Array<Object>
Returns all the persisted entities.
463 464 465 466 467 468 469 |
# File 'lib/epiphy/repository.rb', line 463 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.
593 594 595 |
# File 'lib/epiphy/repository.rb', line 593 def clear @adapter.clear(collection) end |
#count ⇒ Object
Count the entity in this collection
603 604 605 |
# File 'lib/epiphy/repository.rb', line 603 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
336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/epiphy/repository.rb', line 336 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.
439 440 441 442 443 444 445 446 447 |
# File 'lib/epiphy/repository.rb', line 439 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.
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/epiphy/repository.rb', line 493 def find(id) entity_id = nil if id.is_a? Epiphy::Entity raise TypeError, "Expecting an string, primitve value" end if id.is_a?(String) || id.is_a?(Integer) entity_id = id else entity_id = id.id if id.respond_to? :id end raise Epiphy::Model::EntityIdNotFound, "Missing entity id" if entity_id.nil? 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.
537 538 539 540 541 542 543 544 |
# File 'lib/epiphy/repository.rb', line 537 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.
571 572 573 574 575 576 577 |
# File 'lib/epiphy/repository.rb', line 571 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.
304 305 306 |
# File 'lib/epiphy/repository.rb', line 304 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.
391 392 393 394 395 396 397 |
# File 'lib/epiphy/repository.rb', line 391 def update(entity) if entity.id @adapter.update(collection, to_document(entity)) else raise Epiphy::Model::NonPersistedEntityError end end |