Class: ROM::Repository::RelationProxy

Inherits:
Object
  • Object
show all
Includes:
Options, Wrap, Relation::Materializable, RelationProxy::Combine
Defined in:
lib/rom/repository/relation_proxy.rb,
lib/rom/repository/relation_proxy/wrap.rb

Overview

RelationProxy decorates a relation and automatically generates mappers that will map raw tuples into rom structs

Relation proxies are being registered within repositories so typically there’s no need to instantiate them manually.

Defined Under Namespace

Modules: Wrap

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wrap

#wrap, #wrap_parent, #wrapped

Constructor Details

#initialize(relation, options = {}) ⇒ 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.

Returns a new instance of RelationProxy.



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

def initialize(relation, options = {})
  super
  @relation = relation
  @name = relation.name.with(options[:name])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

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.

Forward to relation and wrap it with proxy if response was a relation too

TODO: this will be simplified once ROM::Relation has lazy-features built-in

and ROM::Lazy is gone


210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/rom/repository/relation_proxy.rb', line 210

def method_missing(meth, *args, &block)
  if relation.respond_to?(meth)
    result = relation.__send__(meth, *args, &block)

    if result.kind_of?(Relation::Materializable) && !result.is_a?(Relation::Loaded)
      __new__(result)
    else
      result
    end
  else
    super
  end
end

Instance Attribute Details

#nameObject (readonly)



34
35
36
# File 'lib/rom/repository/relation_proxy.rb', line 34

def name
  @name
end

#relationObject (readonly)



30
31
32
# File 'lib/rom/repository/relation_proxy.rb', line 30

def relation
  @relation
end

Instance Method Details

#adapterSymbol

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.

Returns The wrapped relation’s adapter identifier ie :sql or :http.

Returns:

  • (Symbol)

    The wrapped relation’s adapter identifier ie :sql or :http



120
121
122
# File 'lib/rom/repository/relation_proxy.rb', line 120

def adapter
  relation.class.adapter
end

#call(*args) ⇒ Object

Materializes wrapped relation and sends it through a mapper

For performance reasons a combined relation will skip mapping since we only care about extracting key values for combining



49
50
51
# File 'lib/rom/repository/relation_proxy.rb', line 49

def call(*args)
  ((combine? || composite?) ? relation : (relation >> mapper)).call(*args)
end

#combine?Boolean

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.

Returns if this relation is combined aka a relation graph

Returns:

  • (Boolean)


95
96
97
# File 'lib/rom/repository/relation_proxy.rb', line 95

def combine?
  meta[:combine_type]
end

#composite?Boolean

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 if this relation is a composite

Returns:

  • (Boolean)


104
105
106
# File 'lib/rom/repository/relation_proxy.rb', line 104

def composite?
  relation.is_a?(Relation::Composite)
end

#map_with(*names) ⇒ RelationProxy Also known as: as

Maps the wrapped relation with other mappers available in the registry

Parameters:

  • *names (Array<Symbol, Class>)

    Either a list of mapper identifiers or a custom model class

Returns:

  • (RelationProxy)

    A new relation proxy with pipelined relation



61
62
63
64
65
66
67
# File 'lib/rom/repository/relation_proxy.rb', line 61

def map_with(*names)
  if names.size == 1 && names[0].is_a?(Class)
    with(meta: meta.merge(model: names[0]))
  else
    names.reduce(self) { |a, e| a >> relation.mappers[e] }
  end
end

#mapperROM::Mapper

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.

Infers a mapper for the wrapped relation

Returns:

  • (ROM::Mapper)


75
76
77
# File 'lib/rom/repository/relation_proxy.rb', line 75

def mapper
  mappers[to_ast]
end

#metaHash

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.

Returns meta info for the wrapped relation

Returns:

  • (Hash)


113
114
115
# File 'lib/rom/repository/relation_proxy.rb', line 113

def meta
  options[:meta]
end

#respond_to_missing?(meth, _include_private = false) ⇒ Boolean

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.

Returns:

  • (Boolean)


146
147
148
# File 'lib/rom/repository/relation_proxy.rb', line 146

def respond_to_missing?(meth, _include_private = false)
  relation.respond_to?(meth) || super
end

#to_astArray

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.

Returns AST for the wrapped relation

Returns:

  • (Array)


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rom/repository/relation_proxy.rb', line 129

def to_ast
  @to_ast ||=
    begin
      attr_ast = (attributes - wraps_attributes).map { |name|
        [:attribute, name]
      }

      meta = options[:meta].merge(dataset: base_name.dataset)
      meta.delete(:wraps)

      header = attr_ast + nodes_ast + wraps_ast

      [:relation, [base_name.relation, meta, [:header, header]]]
    end
end

#with(new_options) ⇒ 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.

Returns a new instance with new options

Parameters:

  • new_options (Hash)

Returns:



86
87
88
# File 'lib/rom/repository/relation_proxy.rb', line 86

def with(new_options)
  __new__(relation, new_options)
end