Class: ROM::Associations::ManyToMany

Inherits:
Abstract
  • Object
show all
Defined in:
lib/rom/associations/many_to_many.rb

Overview

Abstract many-to-many association type

Constant Summary

Constants included from Memoizable

Memoizable::MEMOIZED_HASH

Instance Attribute Summary collapse

Attributes inherited from Abstract

#definition, #relations, #source, #target

Attributes included from Memoizable

#__memoized__

Instance Method Summary collapse

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

extended

Methods included from Memoizable

included

Constructor Details

#initializeManyToMany

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.



15
16
17
18
# File 'lib/rom/associations/many_to_many.rb', line 15

def initialize(*)
  super
  @join_relation = relations[through]
end

Instance Attribute Details

#join_relationObject (readonly)



12
13
14
# File 'lib/rom/associations/many_to_many.rb', line 12

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

Parameters:

  • children (Array<Hash>)

    An array with child tuples

  • parent (Array, Hash)

    An array with parent tuples or a single tuple

Returns:

  • (Array<Hash>)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rom/associations/many_to_many.rb', line 64

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

#callObject

This method is abstract.

Adapters should implement this method

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/rom/associations/many_to_many.rb', line 25

def call(*)
  raise NotImplementedError
end

#foreign_keySymbol

Return configured or inferred FK name

Returns:

  • (Symbol)


34
35
36
# File 'lib/rom/associations/many_to_many.rb', line 34

def foreign_key
  definition.foreign_key || join_relation.foreign_key(source.name)
end

#parent_combine_keysHash<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

Returns:

  • (Hash<Symbol=>Symbol>)


52
53
54
# File 'lib/rom/associations/many_to_many.rb', line 52

def parent_combine_keys
  target.associations[source.name].combine_keys.to_a.flatten(1)
end

#throughSymbol

Return join-relation name

Returns:

  • (Symbol)


43
44
45
# File 'lib/rom/associations/many_to_many.rb', line 43

def through
  definition.through
end