Class: ROM::Repository::LoadingProxy

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

Overview

LoadingProxy decorates a relation and automatically generate mappers that will map raw tuples into structs

Defined Under Namespace

Modules: Combine, Wrap

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Wrap

#wrap, #wrap_parent, #wrapped

Methods included from Combine

#combine, #combine_children, #combine_keys, #combine_method, #combine_parents, #combine_tuple_key, #combined

Constructor Details

#initialize(relation, options = {}) ⇒ LoadingProxy

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



28
29
30
31
# File 'lib/rom/repository/loading_proxy.rb', line 28

def initialize(relation, options = {})
  super
  @relation = relation
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ 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


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rom/repository/loading_proxy.rb', line 158

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

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

Instance Attribute Details

#relationObject (readonly)



25
26
27
# File 'lib/rom/repository/loading_proxy.rb', line 25

def relation
  @relation
end

Instance Method Details

#call(*args) ⇒ Object

Materialize wrapped relation and send it through a mapper

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



39
40
41
# File 'lib/rom/repository/loading_proxy.rb', line 39

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.

Return if this relation is combined

Returns:

  • (Boolean)


101
102
103
# File 'lib/rom/repository/loading_proxy.rb', line 101

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)


110
111
112
# File 'lib/rom/repository/loading_proxy.rb', line 110

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

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

Map this relation with other mappers too



46
47
48
49
# File 'lib/rom/repository/loading_proxy.rb', line 46

def map_with(*names)
  mappers = [mapper]+names.map { |name| relation.mappers[name] }
  mappers.reduce(self) { |a, e| a >> e }
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.

Infer a mapper for the relation

Returns:

  • (ROM::Mapper)


78
79
80
# File 'lib/rom/repository/loading_proxy.rb', line 78

def mapper
  mapper_builder[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.

Return meta info for this relation

Returns:

  • (Hash)


119
120
121
# File 'lib/rom/repository/loading_proxy.rb', line 119

def meta
  options[:meta]
end

#nodesArray<LoadingProxy>

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 all nodes that this relation combines

Returns:



128
129
130
# File 'lib/rom/repository/loading_proxy.rb', line 128

def nodes
  relation.respond_to?(:nodes) ? relation.nodes : []
end

#respond_to_missing?(name, 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)


83
84
85
# File 'lib/rom/repository/loading_proxy.rb', line 83

def respond_to_missing?(name, include_private = false)
  relation.respond_to?(name) || 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.

Return AST for this relation

Returns:

  • (Array)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rom/repository/loading_proxy.rb', line 57

def to_ast
  attr_ast = attributes.map { |name| [:attribute, name] }

  node_ast = nodes.map(&:to_ast)
  wrap_ast = wraps.map(&:to_ast)

  wrap_attrs = wraps.flat_map { |wrap|
    wrap.attributes.map { |c| [:attribute, :"#{wrap.base_name}_#{c}"] }
  }

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

  [:relation, name, [:header, (attr_ast - wrap_attrs) + node_ast + wrap_ast], meta]
end

#with(new_options) ⇒ LoadingProxy

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 new instance with new options

Returns:



92
93
94
# File 'lib/rom/repository/loading_proxy.rb', line 92

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