Module: Epiphy::Repository::ClassMethods

Includes:
Helper
Defined in:
lib/epiphy/repository.rb

Overview

Since:

  • 0.1.0

Instance Method Summary collapse

Methods included from Helper

#find_by

Instance Method Details

#adapter=(adapter) ⇒ Object

Assigns an adapter.

Epiphy::Repository is shipped with an adapters:

* Rethinkdb

Examples:


class UserRepository
  include Epiphy::Repository
end

# Adapter is set by a shared adapter by default. Unless you want 
to change, you shoul not need this
adapter = Epiphy::Adapter::Rethinkdb.new aconnection, adb
UserRepository.adapter = adapter

Parameters:

  • adapter (Object)

    an object that implements Epiphy::Model::Adapters::Abstract interface

See Also:

Since:

  • 0.1.0



244
245
246
# File 'lib/epiphy/repository.rb', line 244

def adapter=(adapter)
  @adapter = adapter
end

#allArray<Object>

Returns all the persisted entities.

Examples:

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.all # => [ #<Article:0x007f9b19a60098> ]

Returns:

  • (Array<Object>)

    the result of the query

Since:

  • 0.1.0



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

#clearObject

Deletes all the records from the current collection.

Execute a ‘r.table().delete()` on RethinkDB level.

Examples:

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.clear # deletes all the records

Since:

  • 0.1.0



572
573
574
# File 'lib/epiphy/repository.rb', line 572

def clear
  @adapter.clear(collection)
end

#countObject

Count the entity in this collection

Parameters:

  • void

Returns:

  • Interget

Since:

  • 0.2.0



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

Examples:

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

article = Article.new(title: 'Introducing Epiphy::Model')
article.id # => nil

ArticleRepository.create(article) # creates a record
article.id # => 23

ArticleRepository.create(article) # no-op

Parameters:

  • entity (#id, #id=)

    the entity to create

Returns:

  • (Object)

    the entity

See Also:

  • Epiphy::Repository#persist

Since:

  • 0.1.0



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.message
  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.

Examples:

With a persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

article = ArticleRepository.find(23)
article.id # => 23

ArticleRepository.delete(article) # deletes the record

With a non persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

article = Article.new(title: 'Introducing Epiphy::Model')
article.id # => nil

ArticleRepository.delete(article) # raises Epiphy::Model::NonPersistedEntityError

Parameters:

  • entity (#id)

    the entity to delete

Returns:

  • (Object)

    the entity

Raises:

See Also:

Since:

  • 0.1.0



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.

Examples:

With a persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.find(9) # => raises Epiphy::Model::EntityNotFound

Parameters:

  • id (Object)

    the identity of the entity

Returns:

  • (Object)

    the result of the query

Raises:

See Also:

Since:

  • 0.1.0



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.

Examples:

With at least one persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.first # => #<Article:0x007f8c71d98a28>

With an empty collection

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.first # => nil

Returns:

  • (Object, nil)

    the result of the query

See Also:

  • Epiphy::Repository#last

Since:

  • 0.1.0



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.

Examples:

With at least one persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.last # => #<Article:0x007f8c71d98a28>

With an empty collection

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

ArticleRepository.last # => nil

Returns:

  • (Object, nil)

    the result of the query

See Also:

  • Epiphy::Repository#last

Since:

  • 0.1.0



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.

Examples:

With a non persisted entity

require 'epiphy'

class ArticleRepository
  include Epiphy::Repository
end

article = Article.new(title: 'Introducing Epiphy::Model')
article.id # => nil

ArticleRepository.persist(article) # creates a record
article.id # => 23

With a persisted entity

require 'epiphy'

class ArticleRepository
  include Epiphy::Repository
end

article = ArticleRepository.find(23)
article.id # => 23

article.title = 'Launching Epiphy::Model'
ArticleRepository.persist(article) # updates the record

article = ArticleRepository.find(23)
article.title # => "Launching Epiphy::Model"

Parameters:

  • entity (#id, #id=)

    the entity to persist

Returns:

  • (Object)

    the entity

See Also:

  • Epiphy::Repository#create
  • Epiphy::Repository#update

Since:

  • 0.1.0



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.

Examples:

With a persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

article = ArticleRepository.find(23)
article.id # => 23
article.title = 'Launching Epiphy::Model'

ArticleRepository.update(article) # updates the record

With a non persisted entity

require 'epiphy/model'

class ArticleRepository
  include Epiphy::Repository
end

article = Article.new(title: 'Introducing Epiphy::Model')
article.id # => nil

ArticleRepository.update(article) # raises Epiphy::Model::NonPersistedEntityError

Parameters:

  • entity (#id)

    the entity to update

Returns:

  • (Object)

    the entity

Raises:

See Also:

Since:

  • 0.1.0



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