Sha256: 914039a3d9fd354d0d1d6521509a95206fdc0028cce2b8b20e6b3b02f686fa13

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

require "virtus"
require "forwardable"

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

    attribute :q,       String
    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 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(**opts)
      CSVQueryResult.new(self, **opts)
    end

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

    def query_params
      QueryParams.new(self)
    end

    def build(&block)
      QueryBuilder.new(self, &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

6 entries across 6 versions & 1 rubygems

Version Path
ddr-models-3.0.0.rc4 lib/ddr/index/query.rb
ddr-models-3.0.0.rc3 lib/ddr/index/query.rb
ddr-models-3.0.0.rc2 lib/ddr/index/query.rb
ddr-models-3.0.0.rc1 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.22 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.21 lib/ddr/index/query.rb