Sha256: b88a1a32e44f829894be5043807d52795699d66b34e78deff92b6632fca6df02

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Esse
  class Repository
    module ClassMethods
      def import(**kwargs)
        index.import(repo_name, **kwargs)
      end

      def update_documents_attribute(name, *ids_or_doc_headers, **kwargs)
        batch = documents_for_lazy_attribute(name, *ids_or_doc_headers)
        return if batch.empty?

        index.bulk(**kwargs, update: batch)
      end

      def documents_for_lazy_attribute(name, *ids_or_doc_headers)
        unless lazy_document_attribute?(name)
          raise ArgumentError, <<~MSG
            The attribute `#{name}` is not defined as a lazy document attribute.

            Define the attribute as a lazy document attribute using the `lazy_document_attribute` method.
          MSG
        end

        docs = LazyDocumentHeader.coerce_each(ids_or_doc_headers)
        return [] if docs.empty?

        arr = []
        result = fetch_lazy_document_attribute(name).call(docs)
        return [] unless result.is_a?(Hash)

        result.each do |key, datum|
          doc = docs.find { |d| d == key || d.id == key }
          next unless doc

          arr << doc.to_doc(name => datum)
        end
        arr
      end
    end

    extend ClassMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
esse-0.3.0 lib/esse/repository/documents.rb