Class: ROM::Repository::RelationProxy
- Inherits:
-
Object
- Object
- ROM::Repository::RelationProxy
- 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
- #name ⇒ Object readonly
- #relation ⇒ Object readonly
Instance Method Summary collapse
-
#adapter ⇒ Symbol
private
The wrapped relation’s adapter identifier ie :sql or :http.
-
#call(*args) ⇒ Object
Materializes wrapped relation and sends it through a mapper.
-
#combine? ⇒ Boolean
private
Returns if this relation is combined aka a relation graph.
-
#composite? ⇒ Boolean
private
Return if this relation is a composite.
-
#initialize(relation, options = {}) ⇒ RelationProxy
constructor
private
A new instance of RelationProxy.
-
#map_with(*names) ⇒ RelationProxy
(also: #as)
Maps the wrapped relation with other mappers available in the registry.
-
#mapper ⇒ ROM::Mapper
private
Infers a mapper for the wrapped relation.
-
#meta ⇒ Hash
private
Returns meta info for the wrapped relation.
- #respond_to_missing?(meth, _include_private = false) ⇒ Boolean private
-
#to_ast ⇒ Array
private
Returns AST for the wrapped relation.
-
#with(new_options) ⇒ RelationProxy
private
Returns a new instance with new options.
Methods included from Wrap
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, = {}) super @relation = relation @name = relation.name.with([: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
#name ⇒ Object (readonly)
34 35 36 |
# File 'lib/rom/repository/relation_proxy.rb', line 34 def name @name end |
#relation ⇒ Object (readonly)
30 31 32 |
# File 'lib/rom/repository/relation_proxy.rb', line 30 def relation @relation end |
Instance Method Details
#adapter ⇒ Symbol
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.
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
95 96 97 |
# File 'lib/rom/repository/relation_proxy.rb', line 95 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
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
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: .merge(model: names[0])) else names.reduce(self) { |a, e| a >> relation.mappers[e] } end 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.
Infers a mapper for the wrapped relation
75 76 77 |
# File 'lib/rom/repository/relation_proxy.rb', line 75 def mapper mappers[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.
Returns meta info for the wrapped relation
113 114 115 |
# File 'lib/rom/repository/relation_proxy.rb', line 113 def [: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.
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_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.
Returns AST for the wrapped relation
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].merge(dataset: base_name.dataset) .delete(:wraps) header = attr_ast + nodes_ast + wraps_ast [:relation, [base_name.relation, , [: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
86 87 88 |
# File 'lib/rom/repository/relation_proxy.rb', line 86 def with() __new__(relation, ) end |