Sha256: 42cb458de14036c07bb10e982a3b7da08c638be19db89811226bda1e09b6d35d

Contents?: true

Size: 1.13 KB

Versions: 15

Compression:

Stored size: 1.13 KB

Contents

require "virtus"
require "forwardable"

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

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

    delegate [:count, :docs, :pids, :each_pid, :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 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

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ddr-models-2.4.12 lib/ddr/index/query.rb
ddr-models-2.4.11 lib/ddr/index/query.rb
ddr-models-2.4.10 lib/ddr/index/query.rb
ddr-models-2.4.9 lib/ddr/index/query.rb
ddr-models-2.4.8 lib/ddr/index/query.rb
ddr-models-2.4.7 lib/ddr/index/query.rb
ddr-models-2.4.6 lib/ddr/index/query.rb
ddr-models-2.4.5 lib/ddr/index/query.rb
ddr-models-2.4.4 lib/ddr/index/query.rb
ddr-models-2.4.3 lib/ddr/index/query.rb
ddr-models-2.4.2 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.4 lib/ddr/index/query.rb
ddr-models-2.4.1 lib/ddr/index/query.rb
ddr-models-2.4.0 lib/ddr/index/query.rb
ddr-models-2.4.0.rc6 lib/ddr/index/query.rb