Sha256: 10a59537c60bb6954489a4fb192a905ce627c13948a084f87b012bb669d9ba0b

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 Bytes

Contents

# frozen_string_literal: true
module Mongoload
  module RelationLoader
    module_function

    def load(doc, metadata)
      docs = doc.auto_include_context.docs.select do |d|
        auto_include?(d, metadata)
      end
      eager_load(metadata, docs)
      docs.include?(doc)
    end

    def eager_load(metadata, docs)
      metadata.relation.eager_load_klass.new([metadata], docs).run
    end

    def auto_include?(doc, metadata)
      metadata.auto_include? && relation?(doc, metadata) && !loaded?(doc, metadata)
    end

    def relation?(doc, metadata)
      doc.relations.values.include?(metadata)
    end

    def loaded?(doc, metadata)
      if [Mongoid::Relations::Referenced::In, Mongoid::Relations::Referenced::One].include?(metadata.relation)
        doc.ivar(metadata.name) != false
      else
        doc.public_send(metadata.name)._loaded?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoload-0.0.3 lib/mongoload/relation_loader.rb