Class: ROM::Associations::ManyToMany
- Defined in:
- lib/rom/associations/many_to_many.rb
Overview
Abstract many-to-many association type
Direct Known Subclasses
Constant Summary
Constants included from Memoizable
Instance Attribute Summary collapse
- #join_relation ⇒ Object readonly
Attributes inherited from Abstract
#definition, #relations, #source, #target
Attributes included from Memoizable
Instance Method Summary collapse
-
#associate(children, parent) ⇒ Array<Hash>
private
Associate child tuples with the provided parent.
-
#call ⇒ Object
abstract
Adapters should implement this method.
-
#foreign_key ⇒ Symbol
Return configured or inferred FK name.
-
#initialize ⇒ ManyToMany
constructor
private
A new instance of ManyToMany.
-
#parent_combine_keys ⇒ Hash<Symbol=>Symbol>
private
Return parent’s relation combine keys.
-
#through ⇒ Symbol
Return join-relation name.
Methods inherited from Abstract
#aliased?, #apply_view, #as, #combine_keys, #key, #name, new, #node, #override?, #prepare, #result, #self_ref?, #view, #wrap
Methods included from Initializer
Methods included from Memoizable
Constructor Details
#initialize ⇒ ManyToMany
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 ManyToMany.
17 18 19 20 |
# File 'lib/rom/associations/many_to_many.rb', line 17 def initialize(*, **) super @join_relation = relations[through] end |
Instance Attribute Details
#join_relation ⇒ Object (readonly)
14 15 16 |
# File 'lib/rom/associations/many_to_many.rb', line 14 def join_relation @join_relation end |
Instance Method Details
#associate(children, parent) ⇒ Array<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.
Associate child tuples with the provided parent
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rom/associations/many_to_many.rb', line 66 def associate(children, parent) ((spk, sfk), (tfk, tpk)) = join_key_map case parent when Array parent.map { |p| associate(children, p) }.flatten(1) else children.map { |tuple| { sfk => tuple.fetch(spk), tfk => parent.fetch(tpk) } } end end |
#call ⇒ Object
Adapters should implement this method
27 28 29 |
# File 'lib/rom/associations/many_to_many.rb', line 27 def call(*) raise NotImplementedError end |
#foreign_key ⇒ Symbol
Return configured or inferred FK name
36 37 38 |
# File 'lib/rom/associations/many_to_many.rb', line 36 def foreign_key definition.foreign_key || join_relation.foreign_key(source.name) end |
#parent_combine_keys ⇒ Hash<Symbol=>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 parent’s relation combine keys
54 55 56 |
# File 'lib/rom/associations/many_to_many.rb', line 54 def parent_combine_keys target.associations[source.name].combine_keys.to_a.flatten(1) end |
#through ⇒ Symbol
Return join-relation name
45 46 47 |
# File 'lib/rom/associations/many_to_many.rb', line 45 def through definition.through end |