Sha256: d979a7f07d4db5be03ea10127a8e58735948dc43a52f79491403652f185c1fe5

Contents?: true

Size: 1.37 KB

Versions: 14

Compression:

Stored size: 1.37 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

    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

14 entries across 14 versions & 1 rubygems

Version Path
ddr-models-3.0.0.beta.20 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.18 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.17 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.16 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.15 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.14 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.13 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.12 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.11 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.10 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.9 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.8 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.7 lib/ddr/index/query.rb
ddr-models-3.0.0.beta.6 lib/ddr/index/query.rb