Class: ROM::Associations::Abstract
- Inherits:
-
Object
- Object
- ROM::Associations::Abstract
- Extended by:
- Initializer
- Includes:
- Memoizable
- Defined in:
- core/lib/rom/associations/abstract.rb
Overview
Abstract association class
Direct Known Subclasses
Instance Attribute Summary collapse
- #__memoized__ ⇒ Object included from Memoizable readonly private
-
#definition ⇒ ROM::Associations::Definition
readonly
Association configuration object.
-
#relations ⇒ ROM::RelationRegistry
readonly
Relation registry.
-
#source ⇒ ROM::SQL::Relation
readonly
The source relation.
-
#target ⇒ ROM::SQL::Relation::Name
readonly
The target relation.
Class Method Summary collapse
-
.new(definition, relations) ⇒ Object
Create an association object.
Instance Method Summary collapse
-
#aliased? ⇒ Boolean
Return if an association has an alias.
-
#apply_view(schema, relation) ⇒ Relation
Applies custom view to the default association view.
-
#as ⇒ Symbol
Return association alias.
-
#combine_keys ⇒ Hash<Symbol=>Symbol>
Return combine keys hash.
-
#foreign_key ⇒ Symbol
Return association foreign key name.
-
#join_key_map ⇒ Array<Symbol>
private
Return names of source PKs and target FKs.
-
#key ⇒ Symbol
Return the name of a key in tuples under which loaded association data are returned.
-
#name ⇒ Symbol
Return association canonical name.
-
#node ⇒ Relation
private
Return target relation configured as a combine node.
-
#override? ⇒ Boolean
Return if a custom view should override default association view.
-
#prepare(target) ⇒ Relation
private
Prepare association's target relation for composition.
-
#result ⇒ Symbol
Return result type.
-
#self_ref? ⇒ Boolean
private
Return if this association's source relation is the same as the target.
-
#view ⇒ Symbol
Return the name of a custom relation view that should be use to extend or override default association view.
-
#wrap ⇒ Relation
private
Return target relation as a wrap node.
Instance Attribute Details
#__memoized__ ⇒ Object (readonly) Originally defined in module Memoizable
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.
#definition ⇒ ROM::Associations::Definition (readonly)
Returns Association configuration object.
19 |
# File 'core/lib/rom/associations/abstract.rb', line 19 param :definition |
#relations ⇒ ROM::RelationRegistry (readonly)
Returns Relation registry.
23 |
# File 'core/lib/rom/associations/abstract.rb', line 23 option :relations, reader: true |
#source ⇒ ROM::SQL::Relation (readonly)
Returns the source relation.
27 |
# File 'core/lib/rom/associations/abstract.rb', line 27 option :source, reader: true |
#target ⇒ ROM::SQL::Relation::Name (readonly)
Returns the target relation.
31 |
# File 'core/lib/rom/associations/abstract.rb', line 31 option :target, reader: true |
Class Method Details
.new(definition, relations) ⇒ Object
Create an association object
39 40 41 42 43 44 45 46 |
# File 'core/lib/rom/associations/abstract.rb', line 39 def self.new(definition, relations) super( definition, relations: relations, source: relations[definition.source.relation], target: relations[definition.target.relation] ) end |
Instance Method Details
#aliased? ⇒ Boolean
Return if an association has an alias
53 54 55 |
# File 'core/lib/rom/associations/abstract.rb', line 53 def aliased? definition.aliased? end |
#apply_view(schema, relation) ⇒ Relation
Applies custom view to the default association view
128 129 130 131 |
# File 'core/lib/rom/associations/abstract.rb', line 128 def apply_view(schema, relation) view_rel = relation.public_send(view) schema.merge(view_rel.schema).uniq(&:key).(view_rel) end |
#as ⇒ Symbol
Return association alias
62 63 64 |
# File 'core/lib/rom/associations/abstract.rb', line 62 def as definition.as end |
#combine_keys ⇒ Hash<Symbol=>Symbol>
Return combine keys hash
Combine keys are used for merging associated data together, typically these are the same as fk<=>pk mapping
141 142 143 |
# File 'core/lib/rom/associations/abstract.rb', line 141 def combine_keys definition.combine_keys || { source_key => target_key } end |
#foreign_key ⇒ Symbol
Return association foreign key name
90 91 92 |
# File 'core/lib/rom/associations/abstract.rb', line 90 def foreign_key definition.foreign_key end |
#join_key_map ⇒ Array<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.
Return names of source PKs and target FKs
150 151 152 |
# File 'core/lib/rom/associations/abstract.rb', line 150 def join_key_map join_keys.to_a.flatten(1).map(&:key) end |
#key ⇒ Symbol
Return the name of a key in tuples under which loaded association data are returned
119 120 121 |
# File 'core/lib/rom/associations/abstract.rb', line 119 def key as || name end |
#name ⇒ Symbol
Return association canonical name
71 72 73 |
# File 'core/lib/rom/associations/abstract.rb', line 71 def name definition.name end |
#node ⇒ Relation
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 target relation configured as a combine node
159 160 161 162 163 164 |
# File 'core/lib/rom/associations/abstract.rb', line 159 def node target.with( name: target.name.as(key), meta: { keys: combine_keys, combine_type: result, combine_name: key } ) end |
#override? ⇒ Boolean
Return if a custom view should override default association view
110 111 112 |
# File 'core/lib/rom/associations/abstract.rb', line 110 def override? definition.override end |
#prepare(target) ⇒ Relation
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.
Prepare association's target relation for composition
184 185 186 187 188 189 190 |
# File 'core/lib/rom/associations/abstract.rb', line 184 def prepare(target) if override? target.public_send(view) else call(target: target) end end |
#result ⇒ Symbol
Return result type
This can be either :one or :many
101 102 103 |
# File 'core/lib/rom/associations/abstract.rb', line 101 def result definition.result end |
#self_ref? ⇒ 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 association's source relation is the same as the target
197 198 199 |
# File 'core/lib/rom/associations/abstract.rb', line 197 def self_ref? source.name.dataset == target.name.dataset end |
#view ⇒ Symbol
Return the name of a custom relation view that should be use to extend or override default association view
81 82 83 |
# File 'core/lib/rom/associations/abstract.rb', line 81 def view definition.view end |
#wrap ⇒ Relation
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 target relation as a wrap node
171 172 173 174 175 176 177 |
# File 'core/lib/rom/associations/abstract.rb', line 171 def wrap target.with( name: target.name.as(key), schema: target.schema.wrap, meta: { wrap: true, combine_name: key } ) end |