class ROM::Associations::ManyToMany

Abstract many-to-many association type

@api public

Attributes

join_relation[R]

@!attribute [r] join_relation

@return [Relation] Intermediate join relation

Public Class Methods

new(*) click to toggle source

@api private

Calls superclass method ROM::Associations::Abstract::new
# File lib/rom/associations/many_to_many.rb, line 17
def initialize(*)
  super
  @join_relation = relations[through]
end

Public Instance Methods

associate(children, parent) click to toggle source

Associate child tuples with the provided parent

@param [Array<Hash>] children An array with child tuples @param [Array,Hash] parent An array with parent tuples or a single tuple

@return [Array<Hash>]

@api private

# File lib/rom/associations/many_to_many.rb, line 67
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(*) click to toggle source

Adapters should implement this method

@abstract

@api public

# File lib/rom/associations/many_to_many.rb, line 28
def call(*)
  raise NotImplementedError
end
foreign_key() click to toggle source

Return configured or inferred FK name

@return [Symbol]

@api public

# File lib/rom/associations/many_to_many.rb, line 37
def foreign_key
  definition.foreign_key || join_relation.foreign_key(source.name)
end
parent_combine_keys() click to toggle source

Return parent's relation combine keys

@return [Hash<Symbol=>Symbol>]

@api private

# File lib/rom/associations/many_to_many.rb, line 55
def parent_combine_keys
  target.associations[source.name].combine_keys.to_a.flatten(1)
end
through() click to toggle source

Return join-relation name

@return [Symbol]

@api public

# File lib/rom/associations/many_to_many.rb, line 46
def through
  definition.through
end

Protected Instance Methods

join_assoc() click to toggle source

Return association for many-to-many-through

@return [Association]

@api protected

# File lib/rom/associations/many_to_many.rb, line 105
def join_assoc
  if join_relation.associations.key?(through.assoc_name)
    join_relation.associations[through.assoc_name]
  else
    join_relation.associations[through.target]
  end
end
join_key_map() click to toggle source

Return a [pk, fk] mapping for source/target relations

@return [Array<Symbol>]

@api protected

# File lib/rom/associations/many_to_many.rb, line 118
def join_key_map
  left = super
  right = join_assoc.join_key_map

  [left, right]
end
source_key() click to toggle source

Primary key name on the source side

@return [Symbol]

@api protected

# File lib/rom/associations/many_to_many.rb, line 87
def source_key
  source.primary_key
end
target_key() click to toggle source

Foreign key name on the target side

@return [Symbol]

@api protected

# File lib/rom/associations/many_to_many.rb, line 96
def target_key
  foreign_key
end