Class: CouchRest::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/couchrest/core/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, name) ⇒ Database

Returns a new instance of Database.



8
9
10
11
12
13
# File 'lib/couchrest/core/database.rb', line 8

def initialize server, name
  @name = name
  @server = server
  @host = server.uri
  @root = "#{host}/#{name}"
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/couchrest/core/database.rb', line 6

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/couchrest/core/database.rb', line 6

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/couchrest/core/database.rb', line 6

def root
  @root
end

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/couchrest/core/database.rb', line 6

def server
  @server
end

Instance Method Details

#action(action, params = nil) ⇒ Object

experimental



44
45
46
47
# File 'lib/couchrest/core/database.rb', line 44

def action action, params = nil
  url = CouchRest.paramify_url "#{@root}/_action/#{action}", params
  CouchRest.get url
end

#bulk_save(docs) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/couchrest/core/database.rb', line 73

def bulk_save docs
  ids, noids = docs.partition{|d|d['_id']}
  uuid_count = [noids.length, @server.uuid_batch_count].max
  noids.each do |doc|
    doc['_id'] = @server.next_uuid(uuid_count)
  end
  CouchRest.post "#{@root}/_bulk_docs", {:docs => docs}
end

#delete(doc) ⇒ Object



82
83
84
85
# File 'lib/couchrest/core/database.rb', line 82

def delete doc
  slug = CGI.escape(doc['_id'])
  CouchRest.delete "#{@root}/#{slug}?rev=#{doc['_rev']}"
end

#delete!Object



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

def delete!
  CouchRest.delete @root
end

#documents(params = nil) ⇒ Object



23
24
25
26
# File 'lib/couchrest/core/database.rb', line 23

def documents params = nil
  url = CouchRest.paramify_url "#{@root}/_all_docs", params
  CouchRest.get url  
end

#fetch_attachment(doc, name) ⇒ Object



54
55
56
57
58
# File 'lib/couchrest/core/database.rb', line 54

def fetch_attachment doc, name
  doc = CGI.escape(doc)
  name = CGI.escape(name)
  RestClient.get "#{@root}/#{doc}/#{name}"
end

#get(id) ⇒ Object



49
50
51
52
# File 'lib/couchrest/core/database.rb', line 49

def get id
  slug = CGI.escape(id) 
  CouchRest.get "#{@root}/#{slug}"
end

#infoObject



19
20
21
# File 'lib/couchrest/core/database.rb', line 19

def info
  CouchRest.get @root
end

#save(doc) ⇒ Object

PUT or POST depending on presence of _id attribute



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/couchrest/core/database.rb', line 61

def save doc
  if doc['_attachments']
    doc['_attachments'] = encode_attachments(doc['_attachments'])
  end
  if doc['_id']
    slug = CGI.escape(doc['_id'])
  else
    slug = doc['_id'] = @server.next_uuid
  end
  CouchRest.put "#{@root}/#{slug}", doc
end

#search(params = nil) ⇒ Object

experimental



39
40
41
42
# File 'lib/couchrest/core/database.rb', line 39

def search params = nil
  url = CouchRest.paramify_url "#{@root}/_search", params
  CouchRest.get url
end

#temp_view(funcs, params = nil) ⇒ Object



28
29
30
31
# File 'lib/couchrest/core/database.rb', line 28

def temp_view funcs, params = nil
  url = CouchRest.paramify_url "#{@root}/_temp_view", params
  JSON.parse(RestClient.post(url, funcs.to_json, {"Content-Type" => 'application/json'}))
end

#to_sObject



15
16
17
# File 'lib/couchrest/core/database.rb', line 15

def to_s
  @root
end

#view(name, params = nil) ⇒ Object



33
34
35
36
# File 'lib/couchrest/core/database.rb', line 33

def view name, params = nil
  url = CouchRest.paramify_url "#{@root}/_view/#{name}", params
  CouchRest.get url
end