Class: ROM::Repository::LoadingProxy
- Inherits:
-
Object
- Object
- ROM::Repository::LoadingProxy
- 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
Instance Attribute Summary collapse
- #relation ⇒ Object readonly
Instance Method Summary collapse
-
#call(*args) ⇒ Object
Materialize wrapped relation and send it through a mapper.
-
#combine? ⇒ Boolean
private
Return if this relation is combined.
-
#composite? ⇒ Boolean
private
Return if this relation is a composite.
-
#initialize(relation, options = {}) ⇒ LoadingProxy
constructor
private
A new instance of LoadingProxy.
-
#map_with(*names) ⇒ Object
(also: #as)
Map this relation with other mappers too.
-
#mapper ⇒ ROM::Mapper
private
Infer a mapper for the relation.
-
#meta ⇒ Hash
private
Return meta info for this relation.
-
#nodes ⇒ Array<LoadingProxy>
private
Return all nodes that this relation combines.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean private
-
#to_ast ⇒ Array
private
Return AST for this relation.
-
#with(new_options) ⇒ LoadingProxy
private
Return new instance with new options.
Methods included from Wrap
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, = {}) 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
#relation ⇒ Object (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
101 102 103 |
# File 'lib/rom/repository/loading_proxy.rb', line 101 def combine? [: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
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 |
#mapper ⇒ ROM::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
78 79 80 |
# File 'lib/rom/repository/loading_proxy.rb', line 78 def mapper mapper_builder[to_ast] end |
#meta ⇒ Hash
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
119 120 121 |
# File 'lib/rom/repository/loading_proxy.rb', line 119 def [:meta] end |
#nodes ⇒ Array<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
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.
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_ast ⇒ Array
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
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].merge(base_name: relation.base_name) .delete(:wraps) [:relation, name, [:header, (attr_ast - wrap_attrs) + node_ast + wrap_ast], ] 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
92 93 94 |
# File 'lib/rom/repository/loading_proxy.rb', line 92 def with() __new__(relation, ) end |