Sha256: 92b35b3d0b6657d2953eaa18efea02e259e89a3fcafc82b881ccc8bf9824ad3e

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require "rom/associations/abstract"

module ROM
  module Associations
    # Abstract one-to-many association
    #
    # @api public
    class OneToMany < Abstract
      # Adapters must implement this method
      #
      # @abstract
      #
      # @api public
      def call(*)
        raise NotImplementedError
      end

      # Return configured or inferred FK name
      #
      # @return [Symbol]
      #
      # @api public
      def foreign_key
        definition.foreign_key || target.foreign_key(source.name)
      end

      # Associate child tuple with a parent
      #
      # @param [Hash] child The child tuple
      # @param [Hash] parent The parent tuple
      #
      # @return [Hash]
      #
      # @api private
      def associate(child, parent)
        pk, fk = join_key_map
        child.merge(fk => parent.fetch(pk))
      end

      protected

      # Return primary key on the source side
      #
      # @return [Symbol]
      #
      # @api protected
      def source_key
        source.schema.primary_key_name
      end

      # Return foreign key name on the target side
      #
      # @return [Symbol]
      #
      # @api protected
      def target_key
        foreign_key
      end

      memoize :foreign_key
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-6.0.0.alpha1 lib/rom/associations/one_to_many.rb