ROM::Repository::Root purpose and usage

Hello,

Can anybody please help me in making understand the purpose of class ROM::Repository::Root and how to use it?

In the official documentation on rom-rb website under Repositories I couldn’t find any information related to that class.

And the API documentation at Class: ROM::Repository::Root — Documentation for rom-rb/rom (main) under Examples illustrates following:

class UserRepo < ROM::Repository[:users]
  commands :create, update: :by_pk, delete: :by_pk
end

It’s confusing that the Examples section for ROM::Repository::Root illustrates extending from ROM::Repository.

Also I posted a related question in my another post at Using ROM 5.3.0 Repository with Hanami 2.0. I would highly appreciate if anybody can share inputs on it as well.

Thanks.

1 Like

Here’s @solnic’s brief explanation of where this came from.

Essentially, given that you have a UserRepo that inherits ROM::Repository[:users], this means that UserRepo#root will be aliased to users. That’s basically all there is to it.

For instance, here’s something I do in my app.

module Entities; end

class Repository < ROM::Repository::Root
  include Deps[container: "persistence.rom"]

  struct_namespace Entities

  def find(id) = root.by_pk(id).one
  def find_by(...) = root.where(...).one
end

That simple alias trick means I am able to write find and find_by once and use them with all repos.

4 Likes