Module: ROM::Repository::ClassInterface

Included in:
ROM::Repository
Defined in:
lib/rom/repository/class_interface.rb

Overview

Class-level APIs for repositories

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Class

Create a root-repository class and set its root relation

Examples:

# where :users is the relation name in your rom container
class UserRepo < ROM::Repository[:users]
end

Parameters:

  • name (Symbol)

    The relation ‘register_as` value

Returns:

  • (Class)

    descendant of ROM::Repository::Root



19
20
21
22
23
24
# File 'lib/rom/repository/class_interface.rb', line 19

def [](name)
  klass = Class.new(self < Repository::Root ? self : Repository::Root)
  klass.relations(name)
  klass.root(name)
  klass
end

#commands(*names, mapper: nil, use: nil, **opts) ⇒ Array<Symbol>

Defines command methods on a root repository

Examples:

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

# with custom command plugin
class UserRepo < ROM::Repository[:users]
  commands :create, plugin: :my_command_plugin
end

# with custom mapper
class UserRepo < ROM::Repository[:users]
  commands :create, mapper: :my_custom_mapper
end

Parameters:

  • names (Array<Symbol>)

    A list of command names

  • :mapper (Hash)

    a customizable set of options

  • :use (Hash)

    a customizable set of options

Returns:

  • (Array<Symbol>)

    A list of defined command names



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rom/repository/class_interface.rb', line 93

def commands(*names, mapper: nil, use: nil, **opts)
  if names.any? || opts.any?
    @commands = names + opts.to_a

    @commands.each do |spec|
      type, *view = Array(spec).flatten

      if view.size > 0
        define_restricted_command_method(type, view, mapper: mapper, use: use)
      else
        define_command_method(type, mapper: mapper, use: use)
      end
    end
  else
    @commands || []
  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.

Inherits configured relations and commands



29
30
31
32
33
34
35
36
# File 'lib/rom/repository/class_interface.rb', line 29

def inherited(klass)
  super

  return if self === Repository

  klass.relations(*relations)
  klass.commands(*commands)
end

#relations(*names) ⇒ Array<Symbol>

Define which relations your repository is going to use

Examples:

class MyRepo < ROM::Repository
  relations :users, :tasks
end

my_repo = MyRepo.new(rom)

my_repo.users
my_repo.tasks

Returns:

  • (Array<Symbol>)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rom/repository/class_interface.rb', line 53

def relations(*names)
  if names.any?
    attr_reader(*names)

    if defined?(@relations)
      @relations.concat(names).uniq!
    else
      @relations = names
    end

    @relations
  else
    @relations
  end
end