Class: Gnouch::Database

Inherits:
Requestor show all
Defined in:
lib/gnouch/database.rb

Instance Method Summary collapse

Methods inherited from Requestor

#block_request, #request, #url

Constructor Details

#initialize(url) ⇒ Database

Returns a new instance of Database.



9
10
11
# File 'lib/gnouch/database.rb', line 9

def initialize(url)
  @url = url
end

Instance Method Details

#<<(doc) ⇒ Object



53
54
55
56
57
58
# File 'lib/gnouch/database.rb', line 53

def <<(doc)
  id = doc["_id"]
  json = doc.to_json
  req = id ? [:put, url(id), {}, json] : [:post, url, {}, json]
  request req
end

#[](id) ⇒ Object



60
61
62
# File 'lib/gnouch/database.rb', line 60

def [](id)
  request [:get, url(id), {}]
end

#[]=(id, doc) ⇒ Object



64
65
66
# File 'lib/gnouch/database.rb', line 64

def []=(id, doc)
  request [:put, url(id), {}, doc.to_json]
end

#all_docsObject



68
69
70
# File 'lib/gnouch/database.rb', line 68

def all_docs()
  self[:_all_docs]
end

#delete(id, rev = nil) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/gnouch/database.rb', line 45

def delete(id, rev=nil)
  unless rev
    doc = request [:get, url(id), {}]
    rev = doc["_rev"]
  end
  request [:delete, [url(id), {:rev => rev}], {}]
end

#design_info(design) ⇒ Object



87
88
89
# File 'lib/gnouch/database.rb', line 87

def design_info(design)
  request [:get, url("_design/#{design}/_info"), {}]
end

#find(design, view, opts = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/gnouch/database.rb', line 91

def find(design, view, opts=nil)
  raw = view(design, view, opts)
  raw["rows"].collect do |row|
    row["value"]
  end
end

#find_docs(design, view, opts = {}) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/gnouch/database.rb', line 107

def find_docs(design, view, opts={})
  opts[:include_docs] = true
  raw = view(design, view, opts)
  raw["rows"].collect do |row|
    row["doc"]
  end
end

#find_first(design, view, opts = nil) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/gnouch/database.rb', line 98

def find_first(design, view, opts=nil)
  raw = view(design, view, opts)
  unless raw["rows"].empty?
    raw["rows"].first["value"]
  else
    nil
  end
end

#find_first_doc(design, view, opts = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/gnouch/database.rb', line 115

def find_first_doc(design, view, opts={})
  opts[:include_docs] = true
  raw = view(design, view, opts)
  unless raw["rows"].empty?
    raw["rows"].first["doc"]
  else
    nil
  end
end

#get(id) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/gnouch/database.rb', line 36

def get(id)
  req = [:get, url(id), {}]
  if block_given?
    yield *block_request(req)
  else
    request req
  end
end

#infoObject



18
19
20
# File 'lib/gnouch/database.rb', line 18

def info()
  request [:get, url, {}]
end

#resetObject



13
14
15
16
# File 'lib/gnouch/database.rb', line 13

def reset()
  request [:delete, url, {}]
  request [:put, url, {}, ""]
end

#save(id, doc = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gnouch/database.rb', line 22

def save(id, doc=nil)
  unless doc
    doc = id
    id = doc["_id"]
  end
  json = doc.to_json
  req = id ? [:put, url(id), {}, json] : [:post, url, {}, json]
  if block_given?
    yield *block_request(req)
  else
    request req
  end
end

#securityObject



72
73
74
# File 'lib/gnouch/database.rb', line 72

def security()
  self[:_security]
end

#view(design, view, opts = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/gnouch/database.rb', line 76

def view(design, view, opts=nil)
  url = "#{@url}/_design/#{design}/_view/#{view}"
  if opts
    [:key, :startkey, :endkey].each do |key|
      opts[key] = opts[key].to_json if opts.has_key? key
    end
    url = [url, opts]
  end
  request [:get, url, {}]
end