Module: Hanami::Repository::Commands

Defined in:
gems/gems/hanami-model-1.3.0/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

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

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

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