Class: Hanami::Model::MappedRelation Private

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/hanami/model/mapped_relation.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Mapped proxy for ROM relations.

It eliminates the need to use #as for repository queries

Since:

  • 1.0.0

Constant Summary collapse

MAPPER_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapper name.

With ROM mapping there is a link between the entity class and a generic reference for it. Example: BookRepository references Book as :entity.

Since:

  • 1.0.0

:entity

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ MappedRelation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MappedRelation.

Since:

  • 1.0.0



30
31
32
33
# File 'lib/hanami/model/mapped_relation.rb', line 30

def initialize(relation)
  @relation = relation
  super(relation.as(self.class.mapper_name))
end

Class Method Details

.mapper_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



24
25
26
# File 'lib/hanami/model/mapped_relation.rb', line 24

def self.mapper_name
  MAPPER_NAME
end

Instance Method Details

#[](attribute) ⇒ ROM::SQL::Attribute

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Access low level relation’s attribute

Examples:

class UserRepository < Hanami::Repository
  def by_matching_name(name)
    users
      .where(users[:name].ilike(name))
      .map_to(User)
      .to_a
  end
end

Parameters:

  • attribute (Symbol)

    the attribute name

Returns:

  • (ROM::SQL::Attribute)

    the attribute

Raises:

Since:

  • 1.2.0



54
55
56
57
58
# File 'lib/hanami/model/mapped_relation.rb', line 54

def [](attribute)
  @relation[attribute]
rescue KeyError => exception
  raise UnknownAttributeError.new(exception.message)
end