Sha256: 248922bd7d92d5cd1284e313342137ae4b7782173424df7197a495987c32d5fd

Contents?: true

Size: 1.26 KB

Versions: 16

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

16 entries across 16 versions & 1 rubygems

Version Path
rom-core-5.4.0 lib/rom/associations/one_to_many.rb
rom-core-5.3.2 lib/rom/associations/one_to_many.rb
rom-core-5.3.1 lib/rom/associations/one_to_many.rb
rom-core-5.3.0 lib/rom/associations/one_to_many.rb
rom-core-5.2.6 lib/rom/associations/one_to_many.rb
rom-core-5.2.5 lib/rom/associations/one_to_many.rb
rom-core-5.2.4 lib/rom/associations/one_to_many.rb
rom-core-5.2.3 lib/rom/associations/one_to_many.rb
rom-core-5.2.2 lib/rom/associations/one_to_many.rb
rom-core-5.2.1 lib/rom/associations/one_to_many.rb
rom-core-5.1.2 lib/rom/associations/one_to_many.rb
rom-core-5.1.1 lib/rom/associations/one_to_many.rb
rom-core-5.1.0 lib/rom/associations/one_to_many.rb
rom-core-5.0.2 lib/rom/associations/one_to_many.rb
rom-core-5.0.1 lib/rom/associations/one_to_many.rb
rom-core-5.0.0 lib/rom/associations/one_to_many.rb