Sha256: 7830f8e21df3e367f49475548570a7a1c0b62e55b73bd5961c7bb0531a5a4d97

Contents?: true

Size: 1.12 KB

Versions: 19

Compression:

Stored size: 1.12 KB

Contents

module Ddr::Index
  class QueryBuilder

    def self.build
      builder = new
      yield builder
      builder.query
    end

    def initialize
      @q       = nil
      @fields  = [ ]
      @filters = [ ]
      @sort    = [ ]
      @rows    = nil
    end

    def query
      Query.new.tap do |qry|
        instance_variables.each do |var|
          qry.instance_variable_set(var, instance_variable_get(var))
        end
      end
    end

    def id(pid)
      q QueryClause.id(pid)
      limit 1
    end

    def filter(*fltrs)
      @filters.push *fltrs
      self
    end

    def fields(*flds)
      @fields.push *flds
      self
    end

    def limit(num)
      @rows = num
      self
    end

    def order_by(field, order)
      @sort << [field, order].join(" ")
      self
    end

    def asc(field)
      order_by field, "asc"
    end

    def desc(field)
      order_by field, "desc"
    end

    def q(q)
      @q = q
      self
    end

    protected

    def method_missing(name, *args, &block)
      if Filter.respond_to? name
        return filter Filter.send(name, *args, &block)
      end
      super
    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ddr-models-3.0.0.beta.3 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.beta.2 lib/ddr/index/query_builder.rb
ddr-models-2.4.0.rc2 lib/ddr/index/query_builder.rb
ddr-models-2.4.0.rc1 lib/ddr/index/query_builder.rb
ddr-models-2.3.2 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.beta.1 lib/ddr/index/query_builder.rb
ddr-models-2.3.1 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.alpha.4 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.alpha.3 lib/ddr/index/query_builder.rb
ddr-models-2.3.0 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.alpha.2 lib/ddr/index/query_builder.rb
ddr-models-3.0.0.alpha.1 lib/ddr/index/query_builder.rb
ddr-models-2.2.0 lib/ddr/index/query_builder.rb
ddr-models-2.1.3 lib/ddr/index/query_builder.rb
ddr-models-2.1.2 lib/ddr/index/query_builder.rb
ddr-models-2.1.1 lib/ddr/index/query_builder.rb
ddr-models-2.1.0 lib/ddr/index/query_builder.rb
ddr-models-2.1.0.rc2 lib/ddr/index/query_builder.rb
ddr-models-2.1.0.rc1 lib/ddr/index/query_builder.rb