Sha256: 5c6b0847eda6b396e286f46bb53f44054e757969230e949f38a41fa8a693891c

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module GraphQL
  module Searchkick
    class LazySearch
      include Enumerable
      extend Forwardable

      SEARCH_ALL = '*'.freeze

      attr_reader :query, :model_class, :options, :limit_value, :offset_value

      def_delegators :load, :results, :hits, :took, :error
      def_delegators :load, :total_count, :current_page, :total_pages
      def_delegators :results, :first, :last, :each, :index
      def_delegators :results, :any?, :empty?, :size, :length, :slice, :[], :to_ary

      def initialize(options, query:, model_class:)
        @query =
          if query.nil? || query.empty?
            SEARCH_ALL
          else
            query
          end
        @model_class = model_class
        @options = options || {}

        if @options.key?(:limit)
          @limit_value = @options[:limit]
        end
      end

      def load
        return @result if defined? @result

        @result = model_class.search(query, options.merge(limit: limit_value, offset: offset_value))

        @result
      end

      def limit=(val)
        @limit_value = val
      end

      def offset=(val)
        @offset_value = val
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-searchkick-0.1.0 lib/graphql/searchkick/lazy_search.rb