Class: Ddr::Index::QueryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ddr/index/query_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryBuilder

Returns a new instance of QueryBuilder.



10
11
12
13
14
15
16
# File 'lib/ddr/index/query_builder.rb', line 10

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



66
67
68
69
70
71
# File 'lib/ddr/index/query_builder.rb', line 66

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

Class Method Details

.build {|builder| ... } ⇒ Object

Yields:

  • (builder)


4
5
6
7
8
# File 'lib/ddr/index/query_builder.rb', line 4

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

Instance Method Details

#asc(field) ⇒ Object



51
52
53
# File 'lib/ddr/index/query_builder.rb', line 51

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

#desc(field) ⇒ Object



55
56
57
# File 'lib/ddr/index/query_builder.rb', line 55

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

#fields(*flds) ⇒ Object



36
37
38
39
# File 'lib/ddr/index/query_builder.rb', line 36

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

#filter(*fltrs) ⇒ Object



31
32
33
34
# File 'lib/ddr/index/query_builder.rb', line 31

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

#id(pid) ⇒ Object



26
27
28
29
# File 'lib/ddr/index/query_builder.rb', line 26

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

#limit(num) ⇒ Object



41
42
43
44
# File 'lib/ddr/index/query_builder.rb', line 41

def limit(num)
  @rows = num
  self
end

#order_by(field, order) ⇒ Object



46
47
48
49
# File 'lib/ddr/index/query_builder.rb', line 46

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

#q(q) ⇒ Object



59
60
61
62
# File 'lib/ddr/index/query_builder.rb', line 59

def q(q)
  @q = q
  self
end

#queryObject



18
19
20
21
22
23
24
# File 'lib/ddr/index/query_builder.rb', line 18

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