Sha256: cba2fcba9a7801e426deee648fe9163f488bc19bd964b3954d433da9dcfdd3f5

Contents?: true

Size: 1.89 KB

Versions: 14

Compression:

Stored size: 1.89 KB

Contents

module Elasticsearch
  module Model

    # Keeps a global registry of classes that include `Elasticsearch::Model`
    #
    class Registry
      def initialize
        @models = []
      end

      # Returns the unique instance of the registry (Singleton)
      #
      # @api private
      #
      def self.__instance
        @instance ||= new
      end

      # Adds a model to the registry
      #
      def self.add(klass)
        __instance.add(klass)
      end

      # Returns an Array of registered models
      #
      def self.all
        __instance.models
      end

      # Adds a model to the registry
      #
      def add(klass)
        @models << klass
      end

      # Returns a copy of the registered models
      #
      def models
        @models.dup
      end
    end

    # Wraps a collection of models when querying multiple indices
    #
    # @see Elasticsearch::Model.search
    #
    class Multimodel
      attr_reader :models

      # @param models [Class] The list of models across which the search will be performed
      #
      def initialize(*models)
        @models = models.flatten
        @models = Model::Registry.all if @models.empty?
      end

      # Get an Array of index names used for retrieving documents when doing a search across multiple models
      #
      # @return [Array] the list of index names used for retrieving documents
      #
      def index_name
        models.map { |m| m.index_name }
      end

      # Get an Array of document types used for retrieving documents when doing a search across multiple models
      #
      # @return [Array] the list of document types used for retrieving documents
      #
      def document_type
        models.map { |m| m.document_type }
      end

      # Get the client common for all models
      #
      # @return Elasticsearch::Transport::Client
      #
      def client
        Elasticsearch::Model.client
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
elasticsearch-model-6.1.2 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-6.1.1 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-6.1.0 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-6.0.0 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-6.0.0.pre lib/elasticsearch/model/multimodel.rb
elasticsearch-model-5.1.0 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-5.0.2 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-2.0.1 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-5.0.1 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-2.0.0 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-5.0.0 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-0.1.9 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-0.1.8 lib/elasticsearch/model/multimodel.rb
elasticsearch-model-0.1.7 lib/elasticsearch/model/multimodel.rb