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



261
262
263
# File 'lib/epiphy/repository.rb', line 261

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



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

#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



593
594
595
# File 'lib/epiphy/repository.rb', line 593

def clear
  @adapter.clear(collection)
end

#countObject

Count the entity in this collection

Parameters:

  • void

Returns:

  • Interget

Since:

  • 0.2.0



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

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



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



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.

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



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.

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



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.

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



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.

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



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.

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



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