Class: Hanami::DB::Repo

Inherits:
ROM::Repository
  • Object
show all
Defined in:
lib/hanami/db/repo.rb

Overview

Since:

  • 2.2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepo

Returns a new instance of Repo.

Since:

  • 2.2.0



36
37
38
39
40
41
42
43
44
# File 'lib/hanami/db/repo.rb', line 36

def initialize(*, **)
  super

  # Repos in Hanami apps infer a root from their class name (e.g. :posts for PostRepo). This
  # means _every_ repo ends up with an inferred root, many of which will not exist as
  # relations. To avoid errors from fetching these non-existent relations, check first before
  # setting the root.
  @root = set_relation(self.class.root) if set_relation?(self.class.root)
end

Instance Attribute Details

#rootObject (readonly)

Since:

  • 2.2.0



26
27
28
# File 'lib/hanami/db/repo.rb', line 26

def root
  @root
end

Class Method Details

.[](root) ⇒ Object

Since:

  • 2.2.0



10
11
12
13
14
15
16
17
18
# File 'lib/hanami/db/repo.rb', line 10

def self.[](root)
  fetch_or_store(root) do
    # Override ROM::Repository.[] logic to ensure repos with explicit roots inherit from
    # Hanami::DB::Repo itself, instead of the plain old ROM::Repository::Root.
    Class.new(self).tap { |klass|
      klass.root(root)
    }
  end
end

.inherited(klass) ⇒ Object

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:

  • 2.2.0



29
30
31
32
# File 'lib/hanami/db/repo.rb', line 29

def self.inherited(klass)
  super
  klass.root(root)
end