Method: Epiphy::Repository::ClassMethods#create

Defined in:
lib/epiphy/repository.rb

#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