Sha256: a86688aa71d34effc1016436214d58e124dc55abec2c8ac4ead191841d4a7f57

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

require 'rom-sql'
require 'hanami/model/associations/belongs_to'
require 'hanami/model/associations/has_many'
require 'hanami/model/associations/has_one'
require 'hanami/model/associations/many_to_many'

module Hanami
  module Model
    # Association factory
    #
    # @since 0.7.0
    # @api private
    class Association
      # Instantiate an association
      #
      # @since 0.7.0
      # @api private
      def self.build(repository, target, subject)
        lookup(repository.root.associations[target])
          .new(repository, repository.root.name.to_sym, target, subject)
      end

      # Translate ROM SQL associations into Hanami::Model associations
      #
      # @since 0.7.0
      # @api private
      # rubocop:disable Metrics/MethodLength
      def self.lookup(association)
        case association
        when ROM::SQL::Association::ManyToMany
          Associations::ManyToMany
        when ROM::SQL::Association::OneToOne
          Associations::HasOne
        when ROM::SQL::Association::OneToMany
          Associations::HasMany
        when ROM::SQL::Association::ManyToOne
          Associations::BelongsTo
        else
          raise "Unsupported association: #{association}"
        end
      end
      # rubocop:enable Metrics/MethodLength
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
hanami-model-1.3.2 lib/hanami/model/association.rb
hanami-model-1.3.1 lib/hanami/model/association.rb
hanami-model-1.3.0 lib/hanami/model/association.rb
hanami-model-1.3.0.beta1 lib/hanami/model/association.rb
hanami-model-1.2.0 lib/hanami/model/association.rb
hanami-model-1.2.0.rc2 lib/hanami/model/association.rb
hanami-model-1.2.0.rc1 lib/hanami/model/association.rb
hanami-model-1.2.0.beta2 lib/hanami/model/association.rb
hanami-model-1.2.0.beta1 lib/hanami/model/association.rb
hanami-model-1.1.0 lib/hanami/model/association.rb
hanami-model-1.1.0.rc1 lib/hanami/model/association.rb
hanami-model-1.1.0.beta3 lib/hanami/model/association.rb
hanami-model-1.1.0.beta2 lib/hanami/model/association.rb
hanami-model-1.1.0.beta1 lib/hanami/model/association.rb