Module: ROM::Repository::RelationProxy::Wrap

Included in:
ROM::Repository::RelationProxy
Defined in:
lib/rom/repository/relation_proxy/wrap.rb

Overview

Provides convenient methods for producing wrapped relations

Instance Method Summary collapse

Instance Method Details

#wrap(*names, **options) ⇒ RelationProxy

Wrap other relations

Examples:

tasks.wrap(owner: [users, user_id: :id])

Parameters:

  • options (Hash)

Returns:



18
19
20
21
22
23
24
25
26
27
# File 'lib/rom/repository/relation_proxy/wrap.rb', line 18

def wrap(*names, **options)
  new_wraps = wraps_from_names(names) + wraps_from_options(options)

  relation = new_wraps.reduce(self) { |a, e|
    name = e.meta[:wrap_from_assoc] ? e.meta[:combine_name] : e.base_name.relation
    a.relation.for_wrap(e.meta.fetch(:keys), name)
  }

  __new__(relation, meta: { wraps: wraps + new_wraps })
end

#wrap_parent(options) ⇒ RelationProxy

Shortcut to wrap parents

Examples:

tasks.wrap_parent(owner: users)

Returns:



37
38
39
40
41
42
43
44
# File 'lib/rom/repository/relation_proxy/wrap.rb', line 37

def wrap_parent(options)
  wrap(
    options.each_with_object({}) { |(name, parent), h|
      keys = combine_keys(parent, relation, :children)
      h[name] = [parent, keys]
    }
  )
end

#wrapped(name, keys, wrap_from_assoc = false) ⇒ RelationProxy

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.

Return a wrapped representation of a loading-proxy relation

This will carry meta info used to produce a correct AST from a relation so that correct mapper can be generated

Returns:



54
55
56
57
58
59
60
61
# File 'lib/rom/repository/relation_proxy/wrap.rb', line 54

def wrapped(name, keys, wrap_from_assoc = false)
  with(
    name: name,
    meta: {
      keys: keys, wrap_from_assoc: wrap_from_assoc, wrap: true, combine_name: name
    }
  )
end

#wraps_from_names(names) ⇒ 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.



69
70
71
72
73
74
# File 'lib/rom/repository/relation_proxy/wrap.rb', line 69

def wraps_from_names(names)
  names.map { |name|
    assoc = associations[name]
    registry[assoc.target.relation].wrapped(name, assoc.combine_keys(__registry__), true)
  }
end

#wraps_from_options(options) ⇒ 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.



64
65
66
# File 'lib/rom/repository/relation_proxy/wrap.rb', line 64

def wraps_from_options(options)
  options.map { |(name, (relation, keys))| relation.wrapped(name, keys) }
end