Sha256: 3a97160b3372e726e18ae67bd4c0c78ab705fa3a25a35ad1b8eacd67c5510ebb

Contents?: true

Size: 1.78 KB

Versions: 43

Compression:

Stored size: 1.78 KB

Contents

require "virtus"
require "forwardable"

module Ddr::Index
  class Query
    include Virtus.model
    extend Forwardable
    extend Deprecation

    attribute :q,       String,                default: '*:*'
    attribute :fields,  Array[FieldAttribute], default: [ ]
    attribute :filters, Array[Filter],         default: [ ]
    attribute :sort,    Array[String],         default: [ ]
    attribute :rows,    Integer

    delegate [:count, :docs, :ids, :each_id, :all] => :result
    delegate :params => :query_params

    def self.build(*args, &block)
      new.tap do |query|
        query.build(*args, &block)
      end
    end

    def initialize(**args, &block)
      super(**args)
      if block_given?
        build(&block)
      end
    end

    def inspect
      "#<#{self.class.name} q=#{q.inspect}, filters=#{filters.inspect}," \
      " sort=#{sort.inspect}, rows=#{rows.inspect}, fields=#{fields.inspect}>"
    end

    def to_s
      URI.encode_www_form(params)
    end

    def pids
      Deprecation.warn(QueryResult, "`pids` is deprecated; use `ids` instead.")
      ids
    end

    def each_pid(&block)
      Deprecation.warn(QueryResult, "`each_pid` is deprecated; use `each_id` instead.")
      each_id(&block)
    end

    def result
      QueryResult.new(self)
    end

    def csv
      CSVQueryResult.new(self)
    end

    def filter_clauses
      filters.map(&:clauses).flatten
    end

    def query_params
      QueryParams.new(self)
    end

    def build(*args, &block)
      QueryBuilder.new(self, *args, &block)
      self
    end

    def ==(other)
      other.instance_of?(self.class) &&
        other.q == self.q &&
        other.fields == self.fields &&
        other.filters == self.filters &&
        other.rows == self.rows &&
        other.sort == self.sort
    end

  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
ddr-core-1.17.0 lib/ddr/index/query.rb
ddr-core-1.16.0 lib/ddr/index/query.rb
ddr-core-1.15.0 lib/ddr/index/query.rb
ddr-core-1.14.0 lib/ddr/index/query.rb
ddr-core-1.13.2 lib/ddr/index/query.rb
ddr-core-1.13.1 lib/ddr/index/query.rb
ddr-core-1.13.0 lib/ddr/index/query.rb
ddr-core-1.12.2 lib/ddr/index/query.rb
ddr-core-1.12.1 lib/ddr/index/query.rb
ddr-core-1.12.0 lib/ddr/index/query.rb
ddr-core-1.11.0 lib/ddr/index/query.rb
ddr-core-1.10.0 lib/ddr/index/query.rb
ddr-core-1.9.0 lib/ddr/index/query.rb
ddr-core-1.8.0 lib/ddr/index/query.rb
ddr-core-1.7.0 lib/ddr/index/query.rb
ddr-core-1.6.6 lib/ddr/index/query.rb
ddr-core-1.6.5 lib/ddr/index/query.rb
ddr-core-1.6.4 lib/ddr/index/query.rb
ddr-core-1.6.3 lib/ddr/index/query.rb
ddr-core-1.6.2 lib/ddr/index/query.rb