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)

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Query.



25
26
27
28
29
30
31
32
# File 'lib/relaxdb/query.rb', line 25

def initialize(view_name, params = {})
  # CouchDB defaults reduce to true when a reduce func is present
  # but returning the map view is typically more useful
  reduce(false)
  
  @view_name = view_name
  params.each { |k, v| send(k, v) }
end

Instance Method Details

#keys(keys = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/relaxdb/query.rb', line 34

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

#merge(paginate_params) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/relaxdb/query.rb', line 69

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 methods.include? method_name
  end
end

#raw(val = nil) ⇒ Object

If set to true RelaxDB.view will return unprocessed data



44
45
46
47
48
49
50
51
# File 'lib/relaxdb/query.rb', line 44

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

#view_pathObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/relaxdb/query.rb', line 53

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