Class: RelaxDB::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxdb/query.rb

Overview

A Query is used to build the query string made against a view All parameter values are first JSON encoded and then URL encoded Nil values are set to the empty string

Constant Summary collapse

@@params =

keys is not included in the standard param as it is significantly different from the others

%w(key startkey startkey_docid endkey endkey_docid limit update 
descending skip group group_level reduce include_docs batch)

Instance Method Summary collapse

Constructor Details

#initialize(view_name, params = {}) ⇒ Query

Returns a new instance of Query.



26
27
28
29
# File 'lib/relaxdb/query.rb', line 26

def initialize view_name, params = {}      
  @view_name = view_name
  params.each { |k, v| send k, v }
end

Instance Method Details

#keys(keys = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/relaxdb/query.rb', line 31

def keys(keys=nil)
  if keys.nil?
    @keys
  else 
    @keys = { :keys => keys }.to_json
    self
  end
end

#merge(paginate_params) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/relaxdb/query.rb', line 66

def merge(paginate_params)
  paginate_params.instance_variables.each do |pp|
    val = paginate_params.instance_variable_get(pp)
    method_name = pp[1, pp.length]
    send(method_name, val) if @@params.include? method_name
  end
end

#raw(val = nil) ⇒ Object

If set to true RelaxDB.view will return unprocessed data



41
42
43
44
45
46
47
48
# File 'lib/relaxdb/query.rb', line 41

def raw(val = nil)
  if val.nil?
    @raw
  else
    @raw = val
    self
  end
end

#view_pathObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/relaxdb/query.rb', line 50

def view_path
  uri = (@view_name =~ /^_/) ? @view_name : "_design/#{RelaxDB.dd}/_view/#{@view_name}"
  
  query = ""
  @@params.each do |param|
    val_set = instance_variable_get("@#{param}_set")
    if val_set
      val = instance_variable_get("@#{param}")
      val = val.to_json unless ["startkey_docid", "endkey_docid"].include?(param)
      query << "&#{param}=#{::CGI::escape(val)}" 
    end
  end

  uri << query.sub(/^&/, "?")
end