Sha256: 50d233bcdf44e4de39c74debf052a9d34f43220819db408b98da5523faf63193

Contents?: true

Size: 954 Bytes

Versions: 10

Compression:

Stored size: 954 Bytes

Contents

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

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
      def self.lookup(association)
        case association
        when ROM::SQL::Association::OneToMany
          Associations::HasMany
        when ROM::SQL::Association::ManyToOne
          Associations::BelongsTo
        else
          raise "Unsupported association: #{association}"
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
hanami-model-1.0.4 lib/hanami/model/association.rb
hanami-model-1.0.3 lib/hanami/model/association.rb
hanami-model-1.0.2 lib/hanami/model/association.rb
hanami-model-1.0.1 lib/hanami/model/association.rb
hanami-model-1.0.0 lib/hanami/model/association.rb
hanami-model-1.0.0.rc1 lib/hanami/model/association.rb
hanami-model-1.0.0.beta3 lib/hanami/model/association.rb
hanami-model-1.0.0.beta2 lib/hanami/model/association.rb
hanami-model-1.0.0.beta1 lib/hanami/model/association.rb
hanami-model-0.7.0 lib/hanami/model/association.rb