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



348
349
350
351
352
# File 'lib/hanami/repository.rb', line 348

def create(*args)
  super
rescue => exception
  raise Hanami::Model::Error.for(exception)
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



396
397
398
399
400
# File 'lib/hanami/repository.rb', line 396

def delete(*args)
  super
rescue => exception
  raise Hanami::Model::Error.for(exception)
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



377
378
379
380
381
# File 'lib/hanami/repository.rb', line 377

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