Class: ROM::Repository::Root

Inherits:
ROM::Repository show all
Extended by:
Dry::Core::ClassAttributes
Defined in:
lib/rom/repository/root.rb

Overview

A specialized repository type dedicated to work with a root relation

This repository type builds commands and aggregates for its root relation

Examples:

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

rom = ROM.container(:sql, 'sqlite::memory') do |conf|
  conf.default.create_table(:users) do
    primary_key :id
    column :name, String
  end
end

user_repo = UserRepo.new(rom)

user = user_repo.create(name: "Jane")

changeset = user_repo.changeset(user.id, name: "Jane Doe")
user_repo.update(user.id, changeset)

user_repo.delete(user.id)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ Root

Returns a new instance of Root.



62
63
64
65
# File 'lib/rom/repository/root.rb', line 62

def initialize(container)
  super
  @root = relations[self.class.root]
end

Instance Attribute Details

#rootObject (readonly)



51
52
53
# File 'lib/rom/repository/root.rb', line 51

def root
  @root
end

Class Method Details

.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.

Sets descendant root relation



56
57
58
59
# File 'lib/rom/repository/root.rb', line 56

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

.rootSymbol .root(identifier) ⇒ Symbol

Get or set repository root relation identifier

This method is automatically used when you define a class using MyRepo shortcut

Overloads:

  • .rootSymbol

    Return root relation identifier

    Returns:

    • (Symbol)
  • .root(identifier) ⇒ Symbol

    Set root relation identifier

    Returns:

    • (Symbol)


47
# File 'lib/rom/repository/root.rb', line 47

defines :root

Instance Method Details

#aggregate(*associations) ⇒ RelationProxy #aggregate(*associations, *assoc_opts) ⇒ RelationProxy #aggregate(options) ⇒ RelationProxy

Compose a relation aggregate from the root relation

Overloads:

  • #aggregate(*associations) ⇒ RelationProxy

    Composes an aggregate from configured associations on the root relation

    Examples:

    user_repo.aggregate(:tasks, :posts)

    Parameters:

    • *associations (Array<Symbol>)

      A list of association names

  • #aggregate(*associations, *assoc_opts) ⇒ RelationProxy

    Composes an aggregate from configured associations and assoc opts on the root relation

    Examples:

    user_repo.aggregate(:tasks, posts: :tags)

    Parameters:

    • *associations (Array<Symbol>)

      A list of association names

    • Association (Hash)

      options for nested aggregates

  • #aggregate(options) ⇒ RelationProxy

    Composes an aggregate by delegating to combine_children method.

    Examples:

    user_repo.aggregate(tasks: :labels)
    user_repo.aggregate(posts: [:tags, :comments])

    Parameters:

    • options (Hash)

      An option hash

    See Also:

Returns:



101
102
103
104
105
106
107
# File 'lib/rom/repository/root.rb', line 101

def aggregate(*args)
  if args.all? { |arg| arg.is_a?(Symbol) }
    root.combine(*args)
  else
    args.reduce(root) { |a, e| a.combine(e) }
  end
end

#changeset(name, *args) ⇒ Object #changeset(data) ⇒ Changeset::Create #changeset(primary_key, data) ⇒ Changeset::Update #changeset(changeset_class) ⇒ Changeset

Overloads:

  • #changeset(name, *args) ⇒ Object

    Delegate to Repository#changeset

  • #changeset(data) ⇒ Changeset::Create

    Builds a create changeset for the root relation

    Examples:

    user_repo.changeset(name: "Jane")

    Parameters:

    • data (Hash)

      New data

    Returns:

  • #changeset(primary_key, data) ⇒ Changeset::Update

    Builds an update changeset for the root relation

    Examples:

    user_repo.changeset(1, name: "Jane Doe")

    Parameters:

    • primary_key (Object)

      Primary key for restricting relation

    Returns:

  • #changeset(changeset_class) ⇒ Changeset

    Return a changeset prepared for repo’s root relation

    Examples:

    changeset = user_repo.changeset(MyChangeset)
    
    changeset.relation == user_repo.root
    # true

    Parameters:

    • changeset_class (Class)

      Your custom changeset class

    Returns:

See Also:



150
151
152
153
154
155
156
157
158
159
# File 'lib/rom/repository/root.rb', line 150

def changeset(*args)
  if args.first.is_a?(Symbol) && relations.key?(args.first)
    super
  elsif args.first.is_a?(Class)
    klass, *rest = args
    super(klass[self.class.root], *rest)
  else
    super(root.name, *args)
  end
end