Class: ROM::Repository::RelationProxy
- Inherits:
-
Object
- Object
- ROM::Repository::RelationProxy
- Extended by:
- Initializer
- Defined in:
- lib/rom/repository/relation_proxy.rb,
lib/rom/repository/relation_proxy/wrap.rb,
lib/rom/repository/relation_proxy/combine.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
Constant Summary collapse
- RelationRegistryType =
Types.Definition(RelationRegistry).constrained(type: RelationRegistry)
Instance Attribute Summary collapse
-
#relation ⇒ Relation, ...
readonly
The decorated relation object.
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(*args) ⇒ RelationProxy
included
from Combine
Combine with other relations.
-
#combine? ⇒ Boolean
private
Returns if this relation is combined aka a relation graph.
-
#combine_children(options) ⇒ RelationProxy
included
from Combine
Shortcut for combining with children which infers the join keys.
-
#combine_parents(options) ⇒ RelationProxy
included
from Combine
Shortcut for combining with parents which infers the join keys.
-
#combined(name, keys, type) ⇒ RelationProxy
included
from Combine
private
Returns a combine representation of a loading-proxy relation.
-
#composite? ⇒ Boolean
private
Return if this relation is a composite.
-
#inspect ⇒ String
Return a string representation of this relation proxy.
-
#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.
-
#name ⇒ ROM::Relation::Name
Relation name.
- #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.
-
#wrap(options) ⇒ RelationProxy
included
from Wrap
Wrap other relations.
-
#wrap_parent(options) ⇒ RelationProxy
included
from Wrap
Shortcut to wrap parents.
-
#wrapped(name, keys) ⇒ RelationProxy
included
from Wrap
private
Return a wrapped representation of a loading-proxy relation.
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
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/rom/repository/relation_proxy.rb', line 220 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 raise NoMethodError, "undefined method `#{meth}' for #{relation.class.name}" end end |
Instance Attribute Details
#relation ⇒ Relation, ... (readonly)
Returns The decorated relation object.
27 |
# File 'lib/rom/repository/relation_proxy.rb', line 27 param :relation |
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.
135 136 137 |
# File 'lib/rom/repository/relation_proxy.rb', line 135 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(*associations) ⇒ RelationProxy #combine(options) ⇒ RelationProxy Originally defined in module Combine
Combine with other relations
#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
119 120 121 |
# File 'lib/rom/repository/relation_proxy.rb', line 119 def combine? [:combine_type] end |
#combine_children(options) ⇒ RelationProxy Originally defined in module Combine
Shortcut for combining with children which infers the join keys
#combine_parents(options) ⇒ RelationProxy Originally defined in module Combine
Shortcut for combining with parents which infers the join keys
#combined(name, keys, type) ⇒ RelationProxy Originally defined in module Combine
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 combine 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
#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
128 129 130 |
# File 'lib/rom/repository/relation_proxy.rb', line 128 def composite? relation.is_a?(Relation::Composite) end |
#inspect ⇒ String
Return a string representation of this relation proxy
90 91 92 |
# File 'lib/rom/repository/relation_proxy.rb', line 90 def inspect %(#<#{relation.class} name=#{name} dataset=#{dataset.inspect}>) end |
#map_with(model) ⇒ RelationProxy #map_with(*mappers) ⇒ RelationProxy Also known as: as
Maps the wrapped relation with other mappers available in the registry
74 75 76 77 78 79 80 81 82 |
# File 'lib/rom/repository/relation_proxy.rb', line 74 def map_with(*names) if names.size == 1 && names[0].is_a?(Class) with(meta: .merge(model: names[0])) elsif names.size > 1 && names.any? { |name| name.is_a?(Class) } raise ArgumentError, 'using custom mappers and a model is not supported' 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
99 100 101 |
# File 'lib/rom/repository/relation_proxy.rb', line 99 def mapper mappers[to_ast] end |
#name ⇒ ROM::Relation::Name
Relation name
39 40 41 |
# File 'lib/rom/repository/relation_proxy.rb', line 39 def name @name == Dry::Initializer::UNDEFINED ? relation.name : relation.name.with(@name) 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.
159 160 161 |
# File 'lib/rom/repository/relation_proxy.rb', line 159 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
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/rom/repository/relation_proxy.rb', line 144 def to_ast @to_ast ||= begin attr_ast = schema.map { |attr| [:attribute, attr] } = self..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
110 111 112 |
# File 'lib/rom/repository/relation_proxy.rb', line 110 def with() __new__(relation, .merge()) end |
#wrap(options) ⇒ RelationProxy Originally defined in module Wrap
Wrap other relations
#wrap_parent(options) ⇒ RelationProxy Originally defined in module Wrap
Shortcut to wrap parents
#wrapped(name, keys) ⇒ RelationProxy Originally defined in module Wrap
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