Class: Throw::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/couchdb/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_name, host = nil) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
# File 'lib/couchdb/server.rb', line 10

def initialize(db_name, host = nil)
  @db_name = db_name
  @host = host || 'localhost:5984'
  @_designs = load_designs
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



75
76
77
# File 'lib/couchdb/server.rb', line 75

def method_missing(name, *args, &block)
  @_designs[name.to_sym] || super
end

Instance Attribute Details

#db_nameObject (readonly)

Returns the value of attribute db_name.



7
8
9
# File 'lib/couchdb/server.rb', line 7

def db_name
  @db_name
end

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/couchdb/server.rb', line 8

def host
  @host
end

Instance Method Details

#delete(path) ⇒ Object



46
47
48
49
50
# File 'lib/couchdb/server.rb', line 46

def delete(path)
  current = get(path)
  data = {_rev: current[:_rev]} if current
  db_request(:delete, path, data)
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/couchdb/server.rb', line 36

def exists?(id)
  !!head(id)
rescue RestClient::ResourceNotFound
  false
end

#find(id) ⇒ Object



28
29
30
# File 'lib/couchdb/server.rb', line 28

def find(id)
  get id
end

#get(path, params = {}) ⇒ Object



56
57
58
# File 'lib/couchdb/server.rb', line 56

def get(path, params = {})
  db_request(:get, path, params)
end

#head(path) ⇒ Object



52
53
54
# File 'lib/couchdb/server.rb', line 52

def head(path)
  db_request(:head, path)
end

#post(path, data = {}) ⇒ Object



64
65
66
# File 'lib/couchdb/server.rb', line 64

def post(path, data = {})
  db_request(:post, path, data)
end

#put(path, data = {}) ⇒ Object



60
61
62
# File 'lib/couchdb/server.rb', line 60

def put(path, data = {})
  db_request(:put, path, data)
end

#reset!Object



68
69
70
71
# File 'lib/couchdb/server.rb', line 68

def reset!
  request(:delete, @db_name)
  request(:put, @db_name)
end

#save(doc, opts = {}) ⇒ Object



32
33
34
# File 'lib/couchdb/server.rb', line 32

def save(doc, opts = {})
  doc.has_key?(:_id) ? put(doc[:_id], doc) : post('', doc)
end

#update_design(name, details = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/couchdb/server.rb', line 16

def update_design(name, details = {})
  details = begin
    get("_design/#{name}").merge(details)
  rescue RestClient::ResourceNotFound
    details
  end

  put("_design/#{name}", details)

  @_designs[name.to_sym] = Design.new(self, details)
end

#uuids(count: 10) ⇒ Object



42
43
44
# File 'lib/couchdb/server.rb', line 42

def uuids(count: 10)
  request(:get, '_uuids', count: count)[:uuids]
end