Module: Hanami::Repository::Commands

Defined in:
lib/hanami/repository.rb

Overview

Extend commands from ROM::Repository with error management

Since:

  • 0.7.0

Instance Method Summary collapse

Instance Method Details

#create(*args) ⇒ Hanami::Entity

Create a new record

Examples:

Create From Hash

user = UserRepository.new.create(name: 'Luca')

Create From Entity

entity = User.new(name: 'Luca')
user   = UserRepository.new.create(entity)

user.id   # => 23
entity.id # => nil - It doesn't mutate original entity

Returns:

Raises:

Since:

  • 0.7.0


359
360
361
362
363
# File 'lib/hanami/repository.rb', line 359

def create(*args)
  super
rescue => e
  raise Hanami::Model::Error.for(e)
end

#delete(*args) ⇒ Hanami::Entity

Delete a record

Examples:

repository = UserRepository.new
user       = repository.create(name: 'Luca')

user       = repository.delete(user.id)

Returns:

Raises:

Since:

  • 0.7.0


407
408
409
410
411
# File 'lib/hanami/repository.rb', line 407

def delete(*args)
  super
rescue => e
  raise Hanami::Model::Error.for(e)
end

#update(*args) ⇒ Hanami::Entity

Update a record

Examples:

Update From Data

repository = UserRepository.new
user       = repository.create(name: 'Luca')

user       = repository.update(user.id, age: 34)

Update From Entity

repository = UserRepository.new
user       = repository.create(name: 'Luca')

entity     = User.new(age: 34)
user       = repository.update(user.id, entity)

user.age  # => 34
entity.id # => nil - It doesn't mutate original entity

Returns:

Raises:

Since:

  • 0.7.0


388
389
390
391
392
# File 'lib/hanami/repository.rb', line 388

def update(*args)
  super
rescue => e
  raise Hanami::Model::Error.for(e)
end